martyj Posted March 18, 2011 Share Posted March 18, 2011 Hello all, Would anyone know how to fix this code to work so that when a user fills out a simple form it update a row in my database? The code below doesn't get the parameters from the form. The $sql = 'UPDATE services SET `Description` = "$description1" WHERE ID=1'; line works perfect only its putting in the $description literally no form parameters?? I have a feeling I need to handle this a different way but other trys with different code were not successful. Using Myphpadmin and mysql 5.1 Thanks in advanced. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?php $con = mysql_connect('localhost', 'DATA_BASE', 'test123'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("DATA_BASE", $con); $sql = 'UPDATE services SET `Description` = "$description1" WHERE ID=1'; mysql_query($sql, $con); if($_POST['Submit']){ $description1=$description1=$_POST['description1']; $price1=$_POST['price1']; mysql_error(); header("location:updates.php"); exit; } mysql_close(); ?> <html> <head> <title>Updates Page</title> </head> <body bgcolor="#A55125"> <div align="center"> <form method="post" action="<? echo $PHP_SELF; ?>"> <span class="forms">Description:</span><br /> <textarea name="description1" cols="40" rows="5" > </textarea><br> <span class="forms">Price:<br /> </span> <input name="price1" type="text" id="price1"><br /> <input type="submit" value="Submit" /> </form> </div> </body> </html> MOD EDIT: . . . tags added. Quote Link to comment https://forums.phpfreaks.com/topic/231047-pulling-out-my-hair-on-php-form/ Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 When posting code, please enclose it within the forum's . . . BBCode tags. Variables are not interpolated when within a single-quoted string. $sql = "UPDATE services SET `Description` = '$description1' WHERE ID=1"; Look at the difference in the syntax highlighting: $sql = 'UPDATE services SET `Description` = "$description1" WHERE ID=1'; Quote Link to comment https://forums.phpfreaks.com/topic/231047-pulling-out-my-hair-on-php-form/#findComment-1189348 Share on other sites More sharing options...
ocpaul20 Posted March 18, 2011 Share Posted March 18, 2011 What I do is to use sprintf when forming queries but that is a personal preference, of course. Anyway, the best way I have found to debug this kind of thing is to use var_dump to print out variables before you use them to make sure that you are getting what you expect. In this case the UPDATE statement. The other way is to use something like PHPadmin or to try out the SQL statements to see if they are working, then place them in your code. Break down problems into small sections of code to get each working then stuff it back into the whole program. Quote Link to comment https://forums.phpfreaks.com/topic/231047-pulling-out-my-hair-on-php-form/#findComment-1189356 Share on other sites More sharing options...
floridaflatlander Posted March 19, 2011 Share Posted March 19, 2011 I'd put my Update in the if submitted condition, you also got to define stuff before you can insert the stuff if($_POST['Submit']){ $description1= $_POST['description1']; $price1=$_POST['price1']; <-- don't know where this goes $sql = "UPDATE services SET Description = '$description1' WHERE ID=1"; mysql_query($sql, $con); Quote Link to comment https://forums.phpfreaks.com/topic/231047-pulling-out-my-hair-on-php-form/#findComment-1189359 Share on other sites More sharing options...
HCProfessionals Posted March 19, 2011 Share Posted March 19, 2011 $description1=$description1=$_POST['description1']; should be $description1=$_POST['description1']; Quote Link to comment https://forums.phpfreaks.com/topic/231047-pulling-out-my-hair-on-php-form/#findComment-1189491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.