PDA

View Full Version : What's wrong with this code?


Friend_Al_23
10-07-2003, 11:37/11:37AM
<?php

$link = mysql_connect("localhost","root","") or die("Connection Error: ".mysql_error());

mysql_select_db("thecofc_ama",$link) or die ("Database Error: ".mysql_error());

$result = mysql_query('INSERT INTO `sc_nom` (`stud`, `studnum`, `email`, `pres`, `vice`, `sec`, `treas`, `proe`, `proi`, `sgt`, `strep`, `ndrep`, `rdrep`, `datetime`) VALUES (`$stud`, `$studnum`, `$email`, `$pres`, `$vice`, `$sec`, `$treas`, `$proe`, `$proi`, `$sgt`, `$strep`, `$ndrep`, `$rdrep`, NOW())',$link) or die("Query Error: ".mysql_error());

mysql_free_result($result);

mysql_close($link);

?>
You can try it out at http://thecofclub.distanthost.com/AMA/sc_nom.php

ihelpyou
10-07-2003, 11:44/11:44AM
Welcome to the forums Friend! :hi:

What do you mean? I don't wish to go there until I know what the code does? One cannot be too careful about going to a page simply because a new member says to go.

Explain further what it is that you want to do or want us to help you with.

JuniorHarris
10-07-2003, 11:49/11:49AM
I don't know anything about php or mysql...but the error message says unknown column $stud.

I would start by looking at that, but I would guess that the column does not exist or the syntax is wrong for the values statement. I'm not sure about mysql, but SQL Server will balk if you character quote a number (ie: '133') for any numeric field on an insert.

If you build the SQL string into a variable, then you can "print" that and see exactly what statement is being sent, and optionally execute that directly against the database to see what happens.

Just something to get you thinking....

Friend_Al_23
10-07-2003, 12:00/12:00PM
@ihelpyou:
See JuniorHarris's reply. He visited it.

@JuniorHarris:
I have a column name stud.
How is the syntax wrong?
Did you tried it by numbers? Anyway, they are varchars.

robwatts
10-07-2003, 12:37/12:37PM
$result = mysql_query('INSERT INTO `sc_nom` VALUES (`$stud`, `$studnum`, `$email`, `$pres`, `$vice`, `$sec`, `$treas`, `$proe`, `$proi`, `$sgt`, `$strep`, `$ndrep`, `$rdrep`, NOW())',$link) or die("Query Error: ".mysql_error());



Try the above.

You should also consider using $_POST[stud] in preference to $stud as some servers have register_globals turned off , which means that stuff like $variablename will not work.


You should also ensure that you use quotes around your form names and values.

<input type=text name=stud>

should be written as

<input type="text" name="stud">

JuniorHarris
10-07-2003, 12:49/12:49PM
Thanks for the assist Rob!~ :up:

Just trying to get you thinking before you got a real response!~ ;)

Friend_Al_23
10-07-2003, 12:54/12:54PM
Thank you guys for your response. Thanks Rob. It kinda works. But it gave me error on the mysql_free_result function so I quoted it. How do I make use of that function?

robwatts
10-07-2003, 13:17/01:17PM
Hi Friend_AI_23

I never use that function, and ( thus far) havent ran into problems as a result.

http://uk.php.net/mysql_free_result tells you a little more on its uses.

Glad to have been able to help a little. :)

Thanks for the assist Rob!~

Anytime JuniorHarris! :)

Dan0
10-07-2003, 21:57/09:57PM
Originally posted by ihelpyou
Welcome to the forums Friend! :hi:

What do you mean? I don't wish to go there until I know what the code does? One cannot be too careful about going to a page simply because a new member says to go.

Explain further what it is that you want to do or want us to help you with.
Doug:

PHP code runs on the server, it can't do anything to ya.

ihelpyou
10-07-2003, 22:15/10:15PM
Cool. I saw the words "or die" in it and that made me pause. :D

Dan0
10-07-2003, 22:29/10:29PM
die() is the generic error/exit function. You pass the error message as an argument to die(), and it's displayed in the browser.

Friend_Al_23
11-07-2003, 13:27/01:27PM
Thanks for your inputs.