views:

311

answers:

2

UPDATE: I narrowed it down, when I got rid of this tag in the header.php file it all works, can someone please explain this.

<script src="#" type="text/javascript"></script>

Hi I'm having quite an annoying issue with my php code. I am trying to update a php database, from a form, when I do this however the fields in the data base become empty after submitting. Please Help! You can view it in action here http://andcreate.com/shoelace/admin/edit1.php click on the lists on the right to edit them and see what happens.

<?php
include("header.php");

echo "<h2>Edit Posts</h2>";

echo "<div id='editNav'>";
echo "<p>Choose Post to Edit</p>";




//////////GET ALL RECORDS AND BUILD A NAV SYSTEM FROM THEM////////
$results = mysql_query("SELECT * FROM shoeData ");



while($row = mysql_fetch_array($results)){




 $id = $row['id'];
 $name = $row['name'];
 $about = $row['about'];

 echo "$date <a href=\"" . $_SERVER['PHP_SELF'] . "?id=$id" . "\">" . substr($name, 0, 40) . " </a> <br/> ";


 }

$thisID = $_GET['id'];

if(!isset($thisID)){
 $thisID = 22;

}




//////////FINISH ALL RECORDS AND BUILD A NAV SYSTEM FROM THEM////////

echo "</div>";



///////IF USER SUBMITS CHANGES UPDATE THE DATABASE//////////
//has user pressed the button
$update = $_GET['update'];

if($update == "yes") {

 $name = $_POST['name'];

 $about =  $_POST['about'];

 $company =  $_POST['company'];

 $buy =  $_POST['buy'];



 //update data for this record
 $sql = "UPDATE shoeData SET 
 name = \"$name\",
 about = \"$about\",
 company = \"$company\",
 buy = \"$buy\"
 WHERE id= $thisID";
 $thisUpdate = mysql_query($sql) or die(mysql_error());


}




///////END IF USER SUBMITS CHANGES UPDATE THE DATABASE//////////




/////////// HERE WE GET THE INFO FOR ONE RECORD ONLY//////// 
$results = mysql_query("SELECT * FROM shoeData WHERE id=$thisID");



while($row = mysql_fetch_array($results)){


 $name = $row['name'];

 $about = $row['about'];

 $company = $row['company'];

 $buy = $row['buy'];

}
//////////////FINISH GETTING INFO FOR ONE RECORD ONLY/////////////



?>

<form name="formS" method="post" action="<?php echo $_SERVER['PHP_SELF']."?id=$thisID&update=yes";?>">

Name
<p>
<input type="text" name="name" id="name" value="<?php echo $name;?>" />
</p>
About
<p>
<input type="text" name="about" id="about" value="<?php echo $about;?>" />
</p>
Company
<p>
<input type="text" name="company" id="company" value="<?php echo $company;?>" />
</p>
Name
<p>
<input type="text" name="buy" id="buy" value="<?php echo $buy;?>" />
</p>



<p>
<input type="submit" name="submit" id="submit"  />
</p>




</form>
<p><a class="delete" href="delete.php?id=<?php echo $thisID;?>">Delete this post</a></p>

<?php
include("footer.php");
?>
A: 

You have $update = $_GET['update'];, but then right after that, you're using $_POST. A given request is either GET or POST, not both - thus whenever $_GET['update'] is set to "yes", there aren't going to be any POST vars set, and thus the update will be done with all of the values it's setting blank.

Chances are you actually meant to use either $_GET or $_POST in both places - since your updates are going through, but are blank, it sounds like you want to use $_GET (though for form submission/updates, you should probably really be using POST instead).

Amber
It *would* be possible to have both in a setting where it's hard-coded into the ACTION for the link or button though, no? e.g. <input type=submit action='http://www.mysite.com/submitform?update=$update'>. Even with a method of POST for the form, there'd still be the one GET variable.
Kaji
A: 

This may seem silly, but are you confusing $_GET and $_POST variables? You use one to check whether to enter the loop, and another to populate the string.

Also, as a minor aside, your SELECT statement towards the end of the snippet can be optimized by adding LIMIT 1 to the end of it, as presumably you're only going to be recalling one entry per id, no?

Kaji
Honestly I got this code from a basic php instructor. However the code worked perfectly in a previous project, not sure now why it submits blank documents. When I use post to enter the loop to see if it was submitted, it never enters it only GET works for me, Im not sure why, but then blank fields are inserted.
Anders Kitson
What's the METHOD set to for the form?
Kaji
the methods set to post, so Im not sure why get even works to enter the loop.
Anders Kitson
Would it be possible to edit the bare bones of the form code (e.g. the form tag and the action statement on what's submitting the form) into the post? That would probably clear up quite a bit
Kaji
I can echo the $name for example inside the update if statement after the button is pushed, and it shows the change in the echo, but as soon as i refresh the fields pertaining to that idea are empty.
Anders Kitson