Jump to content

Update Form Code, something very wrong


wlpywd

Recommended Posts

OK, i guess i need to post code in my question? couldn't quite figure out the other way we were trying, so now trying to have an update page for entries in our database.  Putting together bits and pieces of knowledge and sample code, we are left with something that was working and as we've evolved it, its not working.  Can anyone help and see somthing thats wrong?

 

Thank you,

 

$id=$_GET['id'];
$con = mysql_connect("********", 
                            "*****", 
                            "*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("*****", $con);


$query=" SELECT * FROM usa WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"date");
$last=mysql_result($result,$i,"name");
$fax=mysql_result($result,$i,"color");
$email=mysql_result($result,$i,"discription");


<form method="post">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
Date: <input type="text" name="ud_date" value="<? echo $date; ?>"><br>
Customer: <input type="text" name="ud_name" value="<? echo $name; ?>"><br>
Color: <input type="text" name="ud_color" value="<? echo $color; ?>"><br>
Discription: <input type="text" name="ud_discription" value="<? echo $discription; ?>"><br>
<input type="Submit" value="Update">
</form>

$ud_id=$_POST['ud_id'];
$ud_date=$_POST['ud_date'];
$ud_name=$_POST['ud_name'];
$ud_color=$_POST['ud_color'];
$ud_discription=$_POST['ud_discription'];


$query="UPDATE contacts SET date='$ud_date', name='$ud_name', color='$ud_color', discription='$ud_discription' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

++$i;
}

Link to comment
Share on other sites

Is this the full code? I'm sort of confused because I see a block of HTML in the middle of PHP with no open or closing tags.

 

I suggest putting a die() on the end of the query so you can get some errors and see what is going on.

 

mysql_query($query) or die(mysql_error());

Link to comment
Share on other sites

if you have the PHP in the same script as the form you need to have the script rerun when the submit button is clicked you also need an "action" in your <form> tag.

 

<form method="post" action="<php? $_SERVER['PHP_SELF']; ?>">

 

Also, you need an if statement so the PHP part of the code that gets the info from the form doesn't run until its supposed to

Link to comment
Share on other sites

I've taken out the posting action to a separate file, update5.php:

 

$ud_id=$_POST['ud_id'];
$ud_date=$_POST['ud_date'];
$ud_name=$_POST['ud_name'];
$ud_color=$_POST['ud_color'];
$ud_discription=$_POST['ud_discription'];


$query="UPDATE contacts SET date='$ud_date', name='$ud_name', color='$ud_color', discription='$ud_discription' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

 

and then the update.php file now looks like this with the html tags out of the middle of code, i think.  Is this what it should look like? cuz it is still not working for me... i am getting an error "Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /*************/update.php on line 16"

 

<?
$id=$_GET['id'];
$con = mysql_connect("********", 
                            "*****", 
                            "*****");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("*****", $con);


$query=" SELECT * FROM usa WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"date");
$last=mysql_result($result,$i,"name");
$fax=mysql_result($result,$i,"color");
$email=mysql_result($result,$i,"discription");
?>

<form action="update5.php" method="post">
<input type="hidden" name="ud_id" value="<? echo $id; ?>">
Date: <input type="text" name="ud_date" value="<? echo $date; ?>"><br>
Customer: <input type="text" name="ud_name" value="<? echo $name; ?>"><br>
Color: <input type="text" name="ud_color" value="<? echo $color; ?>"><br>
Discription: <input type="text" name="ud_discription" value="<? echo $discription; ?>"><br>
<input type="Submit" value="Update">
</form>

<?

++$i;
}

?>

Link to comment
Share on other sites

i am looking over this code and i am confused...  I can sorta follow what is suppose to happen, but will this pull each record from the table?? or is there nothing to update from this code?

 

We eventually want, and tried to do at first, the 'update' button to appear in a separate column on the display table page, a page where we display the data from the database, but couldn't get it to work. This code was saved from our first attempt at doing that.  As i look at it, i think the * was used so it would pull for all the entries, i'm not sure how it would work stand alone.  Am i looking at this too long and confusing myself for no reason??

 

also, tried to replace * and id with an entry and still get the same line 16 error!

Link to comment
Share on other sites

isn't that what the <form action="update5.php" method="post"> is for? with update5.php posted above:

 

$ud_id=$_POST['ud_id'];
$ud_date=$_POST['ud_date'];
$ud_name=$_POST['ud_name'];
$ud_color=$_POST['ud_color'];
$ud_discription=$_POST['ud_discription'];


$query="UPDATE contacts SET date='$ud_date', name='$ud_name', color='$ud_color', discription='$ud_discription' WHERE id='$ud_id'";
mysql_query($query);
echo "Record Updated";
mysql_close();

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.