Jump to content

Updating a mysql record error


ipodman

Recommended Posts

Hey guys,

 

Well I have been trying to sort this for the past hour and I have been searching for the solution but if I missed it, it because I am tired.

 

I am trying to update a record in mysql but when I click submit and it goes to update.php it states it has been updated when it has now.

 

here is the form that calls that items from the database

 

<?





if (isset($_GET['pro_id']))
  $pro_id = $_GET['pro_id'];




$db= mysql_connect("localhost", "root");
mysql_select_db("site",$db);

$sql = "SELECT pro_id, name, sale_price, cat_id, description, stock_level FROM stock WHERE pro_id = $pro_id";
$result = mysql_query($sql) or die(mysql_error());

$onlyRecord = mysql_fetch_array($result);
$name = $onlyRecord['name'];
$sale = $onlyRecord['sale_price'];
$cat = $onlyRecord['cat_id'];
$id = $onlyRecord['description'];
$level = $onlyRecord['stock_level'];
  
?>

<html>
<head><title>Edit Record</title></head>
<body>
<form action="update.php" method="POST">
<input type="hidden" value="<? echo $pro_id; ?>">

<table>
<tr><td>ID</td><td><input type="text" name="sitename" value="<? echo $pro_id; ?>"></td></tr>

<tr><td>Name</td><td><input type="text" name="sitename" value="<? echo $name; ?>"></td></tr>

<tr><td>Sale Price</td><td><input type="text" name="sitename" value="<? echo $sale; ?>"></td></tr>
<tr><td>Category ID</td><td><input type="text" name="sitename" value="<? echo $cat; ?>"></td></tr>
<tr><td>description</td><td><input type="text" name="sitename" value="<? echo $id; ?>"></td></tr>
<tr><td>Stock Level</td><td><input type="text" name="sitename" value="<? echo $level; ?>"></td></tr>

</table>

<br />
<input type="submit" value="Update">
</form>
<a href="viewedit.php" target="_self">Go back without saving any changes</a>
</body>
</html>

 

 

And now the update.php which updates the database

 

<?php
$db= mysql_connect("localhost", "root", "");
mysql_select_db("site");

if (isset($_GET['pro_id']))
$pro_id = $_GET['pro_id'];
$name = $_GET['name'];
$sale = $_GET['sale'];
$cat = $_GET['cat'];
$id = $_GET['id'];
$level = $_GET['level'];
  


$sql= ("UPDATE stock SET name='$name', sale_price='$sale', description='$id', stock_level='$level' WHERE pro_id='$pro_id'");
mysql_query($sql) or die(mysql_error());

echo "Record submitted successfully.";
mysql_close();
?>
  
<meta http-equiv="Refresh" content="1; url=viewedit.php"><title>Remove Item</title>
  

 

Any help would be good, thanks

Link to comment
Share on other sites

mysql_select_db("site",$db);

 

What's going on with that?

 

I'm pretty sure mysql_select_db() only accepts one database.

 

I have just edited my first post to show new changes to the code [2nd code] but still have the same error of it saying it has been updated when its not.

Link to comment
Share on other sites

<?php

$db = mysql_connect("localhost", "root", "");
mysql_select_db("site,$db");

if (isset($_GET['pro_id']))
$pro_id = $_GET['pro_id'];
$name = $_GET['name'];
$sale = $_GET['sale'];
$cat = $_GET['cat'];
$id = $_GET['id'];
$level = $_GET['level'];
  


$sql= ("UPDATE `stock` SET name = '$name', sale_price = '$sale', description = '$id', stock_level = '$level' WHERE pro_id = '$pro_id'") or die(mysql_error());
mysql_query($sql) or die(mysql_error());

echo "Record submitted successfully.";
mysql_close();
?>

<meta http-equiv="Refresh" content="1; url=viewedit.php"><title>Remove Item</title>
  

 

Try that ^^ and tell me what you get ?

Link to comment
Share on other sites

<?php

$db = mysql_connect("localhost", "root", "");
mysql_select_db("site,$db");

if (isset($_GET['pro_id']))
$pro_id = $_GET['pro_id'];
$name = $_GET['name'];
$sale = $_GET['sale'];
$cat = $_GET['cat'];
$id = $_GET['id'];
$level = $_GET['level'];
  


$sql= ("UPDATE `stock` SET name = '$name', sale_price = '$sale', description = '$id', stock_level = '$level' WHERE pro_id = '$pro_id'") or die(mysql_error());
mysql_query($sql) or die(mysql_error());

echo "Record submitted successfully.";
mysql_close();
?>

<meta http-equiv="Refresh" content="1; url=viewedit.php"><title>Remove Item</title>
  

 

Try that ^^ and tell me what you get ?

 

Using the code you said i get the error of no database so i change the database code to:-

 

$db = mysql_connect("localhost", "root", "");
mysql_select_db("site");

 

then i go to change a record an dget told it has been changed when it still has not

Link to comment
Share on other sites

just a question, on your Edit Record form does it actually put the mysql records in the value of the text fields ?

 

Yes, it shows the correct data taken from the database

 

Also jackpf I added th emysql error code and nothing came up

Link to comment
Share on other sites

just a question, on your Edit Record form does it actually put the mysql records in the value of the text fields ?

 

Yes, it shows the correct data taken from the database

 

Also jackpf I added th emysql error code and nothing came up

 

ok no problem

 

try this form for your Edit Record page

 

<?php

if (isset($_GET['pro_id']))
  $pro_id = mysql_real_escape_string($_GET['pro_id']);


$db= mysql_connect("localhost", "root");
mysql_select_db("site");

$sql = "SELECT `pro_id`, `name`, `sale_price`, `cat_id`, `description`, `stock_level` FROM stock WHERE pro_id = '$pro_id'" or die(mysql_error());
$result = mysql_query($sql) or die(mysql_error());

$onlyRecord = mysql_fetch_array($result);

$name = $onlyRecord['name'];
$sale = $onlyRecord['sale_price'];
$cat = $onlyRecord['cat_id'];
$id = $onlyRecord['description'];
$level = $onlyRecord['stock_level'];

?>

<html>
<head><title>Edit Record</title></head>
<body>
<form action="update.php" method="POST">
<input type="hidden" value="<?php print $pro_id; ?>">

<table>
<tr><td>ID</td><td><input type="text" name="sitename" value="<?php print $pro_id; ?>"></td></tr>

<tr><td>Name</td><td><input type="text" name="sitename" value="<?php print $name; ?>"></td></tr>

<tr><td>Sale Price</td><td><input type="text" name="sitename" value="<?php print $sale; ?>"></td></tr>
<tr><td>Category ID</td><td><input type="text" name="sitename" value="<?php print $cat; ?>"></td></tr>
<tr><td>description</td><td><input type="text" name="sitename" value="<?php print $id; ?>"></td></tr>
<tr><td>Stock Level</td><td><input type="text" name="sitename" value="<?php print $level; ?>"></td></tr>

</table>

<br />
<input type="submit" name="submit" value="Update">
</form>
<a href="viewedit.php" target="_self">Go back without saving any changes</a>
</body>
</html>

Link to comment
Share on other sites

$db= mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("site") or die(mysql_error());

 

That gave no errors?

 

You obviously have not done as I told you.

 

Like i said, No errors which using your code, but I also added  the password to ("localhost, "root", "") to ensure access to the database. Also tried the script just suggested above. Still the same problem

Link to comment
Share on other sites

$db= mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("site") or die(mysql_error());

 

That gave no errors?

 

You obviously have not done as I told you.

 

Like i said, No errors which using your code, but I also added  the password to ("localhost, "root", "") to ensure access to the database

 

here mate instead of using connection on each page just save this and include it in each page.

 

<?php

$connection = mysql_connect("DB_SERVER","DB_USER","DB_PASS") 
    or die ("Couldn't connect to server."); 
$db = mysql_select_db("DB_NAME", $connection) 
    or die("Couldn't select database."); 

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET); 
  $_POST = array_map('mysql_real_escape_string', $_POST); 
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{  
   $_GET = array_map('stripslashes', $_GET); 
   $_POST = array_map('stripslashes', $_POST); 
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET); 
   $_POST = array_map('mysql_real_escape_string', $_POST); 
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>

Link to comment
Share on other sites

$db= mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("site") or die(mysql_error());

 

That gave no errors?

 

You obviously have not done as I told you.

 

Like i said, No errors which using your code, but I also added  the password to ("localhost, "root", "") to ensure access to the database

 

here mate instead of using connection on each page just save this and include it in each page.

 

<?php

$connection = mysql_connect("DB_SERVER","DB_USER","DB_PASS") 
    or die ("Couldn't connect to server."); 
$db = mysql_select_db("DB_NAME", $connection) 
    or die("Couldn't select database."); 

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET); 
  $_POST = array_map('mysql_real_escape_string', $_POST); 
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{  
   $_GET = array_map('stripslashes', $_GET); 
   $_POST = array_map('stripslashes', $_POST); 
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET); 
   $_POST = array_map('mysql_real_escape_string', $_POST); 
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>

 

Hey,

 

Thats the way I was going to do it, to call it from config file. But still the problem remains, I have tried doing it with another table and it still doe snot change even when it says it has. And no errors are showing

Link to comment
Share on other sites

$db= mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("site") or die(mysql_error());

 

That gave no errors?

 

You obviously have not done as I told you.

 

Like i said, No errors which using your code, but I also added  the password to ("localhost, "root", "") to ensure access to the database

 

here mate instead of using connection on each page just save this and include it in each page.

 

<?php

$connection = mysql_connect("DB_SERVER","DB_USER","DB_PASS") 
    or die ("Couldn't connect to server."); 
$db = mysql_select_db("DB_NAME", $connection) 
    or die("Couldn't select database."); 

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET); 
  $_POST = array_map('mysql_real_escape_string', $_POST); 
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{  
   $_GET = array_map('stripslashes', $_GET); 
   $_POST = array_map('stripslashes', $_POST); 
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET); 
   $_POST = array_map('mysql_real_escape_string', $_POST); 
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>

 

Hey,

 

Thats the way I was going to do it, to call it from config file. But still the problem remains, I have tried doing it with another table and it still doe snot change even when it says it has. And no errors are showing

 

ok put this at the top of your page...

 

ini_set('display_errors', 1);
error_reporting(E_ALL);

Link to comment
Share on other sites

$db= mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("site") or die(mysql_error());

 

That gave no errors?

 

You obviously have not done as I told you.

 

Like i said, No errors which using your code, but I also added  the password to ("localhost, "root", "") to ensure access to the database

 

here mate instead of using connection on each page just save this and include it in each page.

 

<?php

$connection = mysql_connect("DB_SERVER","DB_USER","DB_PASS") 
    or die ("Couldn't connect to server."); 
$db = mysql_select_db("DB_NAME", $connection) 
    or die("Couldn't select database."); 

if(!get_magic_quotes_gpc())
{
  $_GET = array_map('mysql_real_escape_string', $_GET); 
  $_POST = array_map('mysql_real_escape_string', $_POST); 
  $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{  
   $_GET = array_map('stripslashes', $_GET); 
   $_POST = array_map('stripslashes', $_POST); 
   $_COOKIE = array_map('stripslashes', $_COOKIE);
   $_GET = array_map('mysql_real_escape_string', $_GET); 
   $_POST = array_map('mysql_real_escape_string', $_POST); 
   $_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>

 

Hey,

 

Thats the way I was going to do it, to call it from config file. But still the problem remains, I have tried doing it with another table and it still doe snot change even when it says it has. And no errors are showing

 

ok put this at the top of your page...

 

ini_set('display_errors', 1);
error_reporting(E_ALL);

 

Hey,

 

Now I get errors:-

 

on update.php

 

Notice: Undefined index: name in C:\xampplite\htdocs\shop\admin\update.php on line 11

Notice: Undefined index: sale in C:\xampplite\htdocs\shop\admin\update.php on line 12

Notice: Undefined index: cat in C:\xampplite\htdocs\shop\admin\update.php on line 13

 

But no errors on edit.php

Link to comment
Share on other sites

replace all the $_GET 's you have on update.php to. . .

 

if (isset($_POST['pro_id']))
$pro_id = mysql_real_escape_string($_POST['pro_id']);
$name =  mysql_real_escape_string($_POST['name']);
$sale =  mysql_real_escape_string($_POST['sale']);
$cat =   mysql_real_escape_string($_POST['cat']);
$id =    mysql_real_escape_string($_POST['id']);
$level = mysql_real_escape_string($_POST['level']);

Link to comment
Share on other sites

replace all the $_GET 's you have on update.php to. . .

 

if (isset($_POST['pro_id']))
$pro_id = mysql_real_escape_string($_POST['pro_id']);
$name =  mysql_real_escape_string($_POST['name']);
$sale =  mysql_real_escape_string($_POST['sale']);
$cat =   mysql_real_escape_string($_POST['cat']);
$id =    mysql_real_escape_string($_POST['id']);
$level = mysql_real_escape_string($_POST['level']);

 

 

 

With the code you have given above the problem is solved!

Link to comment
Share on other sites

replace all the $_GET 's you have on update.php to. . .

 

if (isset($_POST['pro_id']))
$pro_id = mysql_real_escape_string($_POST['pro_id']);
$name =  mysql_real_escape_string($_POST['name']);
$sale =  mysql_real_escape_string($_POST['sale']);
$cat =   mysql_real_escape_string($_POST['cat']);
$id =    mysql_real_escape_string($_POST['id']);
$level = mysql_real_escape_string($_POST['level']);

 

 

 

With the code you have given above the problem is solved!

 

so its done yes ?

Link to comment
Share on other sites

replace all the $_GET 's you have on update.php to. . .

 

if (isset($_POST['pro_id']))
$pro_id = mysql_real_escape_string($_POST['pro_id']);
$name =  mysql_real_escape_string($_POST['name']);
$sale =  mysql_real_escape_string($_POST['sale']);
$cat =   mysql_real_escape_string($_POST['cat']);
$id =    mysql_real_escape_string($_POST['id']);
$level = mysql_real_escape_string($_POST['level']);

 

 

 

With the code you have given above the problem is solved!

 

so its done yes ?

 

Yes done, Thank you very much!!!!

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.