Jump to content

help Updating Records in MySQL Databases


mus

Recommended Posts

HI im newbie with PHP i have finde code in this website http://www.netadmintools.com/art332.html to make Update in Mysql database.

 

want i test them i have this problem

 

Notice: Undefined variable: HTTP_POST_VARS in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 3

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 3

 

Notice: Undefined variable: formVars in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 10

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 12

1: change form


<html>
<head>
<title>change database</title>
</head>
<body bgcolor="white">
<form method="POST" action="sysdocupdate.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">UID to Update:</font></td>
<td><input type="text" name="if" size=100></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

 

2:sysdocupdate.php

 

<?php

foreach($HTTP_POST_VARS as $varname => $value)
{
$formVars[$varname]=$value;
}

$db1 = mysql_connect("localhost", "host", ""); 
mysql_select_db("polish"); 
$query="SELECT * FROM update WHERE id = \"".$formVars["id"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["raekke"]=$row["raekke"];
$formVars["modelnr"]=$row["modelnr"];
$formVars["navn"]=$row["navn"];
$formVars["priser"]=$row["priser"];
$formVars["display"]=$row["display"];
$formVars["bruger"]=$row["bruger"];
$formVars["salg"]=$row["salg"];
$formVars["usedup"]=$row["usedup"];
$formVars["instock"]=$row["instock"];
$formVars["sysversion"]=$row["sysversion"];
$formVars["totaltstock"]=$row["totaltstock"];
$formVars["id"]=$row["id"];
mysql_close($db1);
?>
<html>
<head>
<title>SystemsDoc Update</title>
</head>
<body bgcolor="white">
<form method="post" action="postupdate.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Row:</font></td>
<td><input type="text" name="raekke" 
value="<? echo $formVars["raekke"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Model_Nr:</font></td>
<td><input type="text" name="modelnr" 
value="<? echo $formVars["modelnr"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Name:</font></td>
<td><input type="text" name="navn" 
value="<? echo $formVars["navn"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">price:</font></td>
<td><input type="text" name="priser" 
value="<? echo $formVars["priser"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">display</font></td>
<td><input type="text" name="display" 
value="<? echo $formVars["display"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Using:</font></td>
<td><input type="text" name="bruger" 
value="<? echo $formVars["bruger"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Sall:</font></td>
<td><input type="text" name="salg" 
value="<? echo $formVars["salg"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Used Up:</font></td>
<td><input type="text" name="usedup" 
value="<? echo $formVars["usedup"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">In Stock:</font></td>
<td><input type="text" name="instock" 
value="<? echo $formVars["instock"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Totalt Stock:</font></td>
<td><input type="text" name="totaltstock" 
value="<? echo $formVars["totaltstock"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">ID:</font></td>
<td><input type="text" name="ID" 
value="<? echo $formVars["ID"]; ?>" size=100></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

 

3:postupdate.php


<?php 

// Save form values to $formVars array 
foreach($HTTP_POST_VARS as $varname => $value) 
{ 
$formVars[$varname]=$value; 
} 

// Include database config file 

// Connect to mysql and select database 
$db1 = mysql_connect("localhost", "host", ""); 
mysql_select_db("polish"); 
// Run query, check for errors 
$query = "UPDATE update SET raekke = '{$formVars["raekke"]}',modelnr = '{$formVars["modelnr"]},navn = '{$formVars["navn"]},priser = '{$formVars["priser"]},display = '{$formVars["display"]},bruger = '{$formVars["bruger"]},salg = '{$formVars["salg"]},usedup = '{$formVars["usedup"]},instock = '{$formVars["instock"]}totaltstock = '{$formVars["totaltstock"]} WHERE id = '{$formVars["id"]}'"; 
if (mysql_query($query) == FALSE) 
{ 
echo "Database Error: " . mysql_error(); 
} 
// Check for affected records 
if (mysql_affected_rows() > 0) 
{ 
echo "Record updated<br><a href=\"sysdocupdate.php\">click here</a> to update another record<br>"; 
} 
else 
{ 
echo "No records updated"; 
} 
// Close database connection 
mysql_close($db1); 
?> 
</body> 
</html> 

 

and i have this problem went i try them

Notice: Undefined variable: HTTP_POST_VARS in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 3

 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 3

 

Notice: Undefined variable: formVars in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 10

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 12

 

please help me thanks

 

[attachment deleted by admin]

Link to comment
Share on other sites

HTTP_POST_VARS is DEPRICATED, change to $_POST

so code looks like this on line 3 of sysdocupdate.php:

foreach($_POST as $varname => $value)

do the same for the other file.

 

you also might want to add a line before that to check if the form was submitted just for debugging purposes:

if(!isset($_POST) || empty($_POST){
echo '<br>Nothing was posted from form.<br>';
}

Link to comment
Share on other sites

thanks WebStyles

 

foreach($_POST as $varname => $value) is work now

 

but im dont understand with if(!isset($_POST) || empty($_POST){echo '<br>Nothing was posted from form.<br>';} can u show me where i put this code in toim sorry but im not undestand sorry please help me

 

i have try this one but i dont understande where i will put you code

 

<?php

foreach($_POST as $varname => $value) 
$_POST[$varname]=$value; 



$db1 = mysql_connect("localhost", "host", ""); 
mysql_select_db("polish"); 
$query="SELECT * FROM update WHERE id = \"".$_POST["id"]."\""; 
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$_POST = array();
$_POST["raekke"]=$row["raekke"];
$_POST["modelnr"]=$row["modelnr"];
$_POST["navn"]=$row["navn"];
$_POST["priser"]=$row["priser"];
$_POST["display"]=$row["display"];
$_POST["bruger"]=$row["bruger"];
$_POST["salg"]=$row["salg"];
$_POST["usedup"]=$row["usedup"];
$$_POST["instock"]=$row["instock"];
$_POST["sysversion"]=$row["sysversion"];
$_POST["totaltstock"]=$row["totaltstock"];
$_POST["id"]=$row["id"];
mysql_close($db1);
?>
<html>
<head>
<title>SystemsDoc Update</title>
</head>
<body bgcolor="white">
<form method="post" action="postupdate.php">
<table>
<col span="1" align="right">
<tr>
<td><font color="blue">Row:</font></td>
<td><input type="text" name="raekke" 
value[color=red]="<? echo $formVars["raekke"]; ?>" [/color] size=100></td>
</tr>
<tr>
<td><font color="blue">Model_Nr:</font></td>
<td><input type="text" name="modelnr" 
value="<? echo $formVars["modelnr"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Name:</font></td>
<td><input type="text" name="navn" 
value="<? echo $formVars["navn"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">price:</font></td>
<td><input type="text" name="priser" 
value="<? echo $formVars["priser"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">display</font></td>
<td><input type="text" name="display" 
value="<? echo $formVars["display"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Using:</font></td>
<td><input type="text" name="bruger" 
value="<? echo $formVars["bruger"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Sall:</font></td>
<td><input type="text" name="salg" 
value="<? echo $formVars["salg"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Used Up:</font></td>
<td><input type="text" name="usedup" 
value="<? echo $formVars["usedup"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">In Stock:</font></td>
<td><input type="text" name="instock" 
value="<? echo $formVars["instock"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">Totalt Stock:</font></td>
<td><input type="text" name="totaltstock" 
value="<? echo $formVars["totaltstock"]; ?>" size=100></td>
</tr>
<tr>
<td><font color="blue">ID:</font></td>
<td><input type="text" name="ID" 
value="<? echo $formVars["ID"]; ?>" size=100></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

 

Notice: Undefined index: id in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 10

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\polish\test\sysdocupdate.php on line 12

 

 

Link to comment
Share on other sites

if(!isset($_POST) || empty($_POST){echo '<br>Nothing was posted from form.<br>';}

is just a code to check if anything has been posted or not, for example, if you try to access sysdocupdate.php directly instead of posting the form, this code would tell you nothing had been posted. you can put it before the foreach() statement.

Link to comment
Share on other sites

Mus,

 

That code checks that there is actually data being passed by the form. In this case you'd want to do something along the lines of:

 

<?php

if(!isset($_POST) || empty($_POST)
{
   echo '<br>Nothing was posted from form.<br>';
}

else 
{
  foreach($_POST as $varname => $value) 
  $_POST[$varname]=$value; 

  ...All your other code here;

}

?>

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.