user2447848
user2447848

Reputation: 289

Can't get php to update mysql

I have a PHP script that I use to update a MySQL database that works fine how I'm using it, I am now using that same script to try to update another database table but I can't get it to write any updates to the table. The script doesn't display any errors, it just moves through the entire process as if it works but posts noting to MySQL. Like I said before, I already use this script with another form and it works perfect. All I have done with the new form is change the variables to match what I have and changed the database table name. All my punctuation is correct as well. I just can't figure out why it wont write the updated info to MySQL.

Here is my code:

<?php
//----------FORM DATA-----------//
$id=$_POST['id'];

$team_name=!empty($_POST['team_name'])? $_POST['team_name'] : '';
$first_name=!empty($_POST['first_name'])? $_POST['first_name'] : '';
$last_name=!empty($_POST['last_name'])? $_POST['last_name'] : '';
$registration=!empty($_POST['registration'])? $_POST['registration'] : '';
$pay_status=!empty($_POST['pay_status'])? $_POST['pay_status'] : '';
$physical=!empty($_POST['physical'])? $_POST['physical'] : '';
$photo=!empty($_POST['photo'])? $_POST['photo'] : '';
$notes=!empty($_POST['notes'])? $_POST['notes'] : '';


//----------CONNECT TO DATABASE----------//
include 'elite_connect.php';


mysql_query("UPDATE cheer SET  team_name='$team_name', first_name='$first_name', last_name='$last_name', registration='$registration', pay_status='$pay_status', physical='$physical', photo='$photo', notes='$notes'
WHERE `id` = '$id'");

mysql_affected_rows();

echo mysql_error();

?>
<html>
<body style="background-color: #C7DFDF">
<center>
<br><br><br>
<form name="results" method="post" action="cheer_results.php" enctype="multipart/form-data" id="cheerresult">
<input type="submit" class="submit" id="cheerresult" style="width: 165px" value="View Results">
</form>
</center>
</body>
</html>

I'm very aware of possible sql injection with this so please don't comment about it, I really don't care about it in this scenario!

Upvotes: 0

Views: 150

Answers (2)

Sean
Sean

Reputation: 12433

Any time you are using $_POST it is always a good idea to check if it is set using isset()

<?php
//----------FORM DATA-----------//

if(isset($_POST['id'])){

   $id=$_POST['id'];

   $team_name=!empty($_POST['team_name'])? $_POST['team_name'] : '';
   $first_name=!empty($_POST['first_name'])? $_POST['first_name'] : '';
   $last_name=!empty($_POST['last_name'])? $_POST['last_name'] : '';
   $registration=!empty($_POST['registration'])? $_POST['registration'] : '';
   $pay_status=!empty($_POST['pay_status'])? $_POST['pay_status'] : '';
   $physical=!empty($_POST['physical'])? $_POST['physical'] : '';
   $photo=!empty($_POST['photo'])? $_POST['photo'] : '';
   $notes=!empty($_POST['notes'])? $_POST['notes'] : '';


   //----------CONNECT TO DATABASE----------//
   include 'elite_connect.php';


   mysql_query("UPDATE cheer SET  team_name='$team_name', first_name='$first_name', last_name='$last_name', registration='$registration', pay_status='$pay_status', physical='$physical', photo='$photo', notes='$notes'
   WHERE `id` = '$id'");

   mysql_affected_rows();

   echo mysql_error();
}
?>

this way, if your code does not execute, you know that id was not posted. Which in this case can be easily resolved with adding the hidden input to your form.

<input type="hidden" name="id" value="<?php echo $data2['id']?>"  />

Upvotes: 1

San
San

Reputation: 624

Im not clear but, can you try this base syntax once?

mysql_query("UPDATE cheer SET  team_name='".$team_name."', first_name='".$first_name."', last_name='".$last_name."', registration='".$registration."', pay_status='".$pay_status."', physical='".$physical."', photo='".$photo."', notes='".$notes."'
WHERE `id` = '".$id."'");

This worked for me many times!

Upvotes: 0

Related Questions