Jump to content

[SOLVED] Why isn't My Database Updating ?


Recommended Posts

I have a code that is supposed to update the shipping status of products on my site, however it everything seems to work except that function. When I go through the program it says the record has been updated, but nothing takes effect. Can someone give me any suggestions?

 

list_shippingrecords.php


<?php
$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>Merchant</strong></td>
<td align="center"><strong>Tracking Number</strong></td>
<td align="center"><strong>Status</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['Date']; ?></td>
<td><?php echo $rows['Merchant']; ?></td>
<td><?php echo $rows['TrackingNum']; ?></td>
<td><?php echo $rows['rest_cat']; ?></td>

// link to update.php and send value of id 
<td align="center"><a href="update.php?ID=<?php echo $rows['ID']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

 

update.php


<?php
$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$ID=$_GET['ID'];


// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE ID='$ID'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>Merchant</strong></td>
<td align="center"><strong>Tracking Number</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="name" type="text" id="name" value="<?php echo $rows['Date']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<?php echo $rows['Merchant']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<?php echo $rows['TrackingNum']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="ID" type="hidden" id="ID" value="<?php echo $rows['ID']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php

// close connection 
mysql_close();

?>

 

 

 

 

update_ac.php


<?php
$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_shippingrecords.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/123765-solved-why-isnt-my-database-updating/
Share on other sites

okay, I've added the variable within the update_ac script ad still no luck.


<?php


$Date=$_POST['Date']; 
$Merchant=$_POST['Merchant'];
$Tracking=$_POST['TrackingNum']; 

$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'";
$result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql");


// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_shippingrecords.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

 

 

Ok, you have no clue what your doing , do you?

 

When you post a var , you retrieve it using $_POST[''];, and what you put in between the braces is the name of the field the value is in.

 

e.g.


<input name="name" type="text" id="name" value="<?php echo $rows['Date']; ?>">
//The post var would be $_POST['name']; , not $_POST['date'];

Vars defined and still no success. Here's what I have now for update_ac.php

 

 

 

<?php
$Date=$_POST['Date'];
$Merchant=$_POST['Merchant'];
$TrackingNum=$_POST['TrackingNum]'

$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'";
$result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql");


// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_shippingrecords.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

 

I dont know if this is the main issue, but you have a syntax error in your last post var.

 

You have $TrackingNum=$_POST['TrackingNum]'

 

it should be

 

$TrackingNum=$_POST['TrackingNum'];

 

 

Also just saw somthing else.

 

I think your if statement that checks $result is a tautology. Since $result wouldn't be null because it would evaluate as true all the time, thus lieing to you. But i am rusty on my php so i could be wrong.

Blade the VARS is fixed, and I still get the error.

 

here is the form...


<?php
$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$ID=$_GET['ID'];


// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE ID='$ID'";
$result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql");


$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td> </td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
<td align="center"> </td>
</tr>
<tr>
<td align="center"> </td>
<td align="center"><strong>Date</strong></td>
<td align="center"><strong>Merchant</strong></td>
<td align="center"><strong>Tracking Number</strong></td>
</tr>
<tr>
<td> </td>
<td align="center"><input name="Date" type="text" id="Date" value="<?php echo $rows['Date']; ?>"></td>
<td align="center"><input name="Merchant" type="text" id="Merchant" value="<?php echo $rows['Merchant']; ?>" size="15"></td>
<td><input name="TrackingNum" type="text" id="TrackingNum" value="<?php echo $rows['TrackingNum']; ?>" size="15"></td>
</tr>
<tr>
<td> </td>
<td><input name="ID" type="hidden" id="ID" value="<?php echo $rows['ID']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td> </td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php

// close connection 
mysql_close();

?>


 

Along with update_ac.php...


<?php
$Date=$_POST['Date'];
$Merchant=$_POST['Merchant'];
$TrackingNum=$_POST['TrackingNum'];

$host="localhost"; // Host name 
$username="intranet"; // Mysql username 
$password="int$123"; // Mysql password 
$db_name="Intranet"; // Database name 
$tbl_name="shipping"; // Table name


// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE $tbl_name SET Date='$Date', Merchant='$Merchant', TrackingNum='$TrackingNum' WHERE ID='$ID'";
$result=mysql_query($sql)or die(mysql_error() ."<p>With Query: $sql");


// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_shippingrecords.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

 

 

 

 

 

Problem Solved. Please forgive my ignorance, I am new to PHP programming, and may find myself in here often. Thanks for the assistance.

 

The problem was that I had the ID var defined as GET instead of POST on a separate page. Once I moved it to the update_ac.php page and switched it to POST, it worked. Thanks for the map and directions.

 

 

 

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.