Jump to content

Pulling out my hair!! on php form...


martyj

Recommended Posts

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.

Link to comment
Share on other sites

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';

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.