please help me i cannot update my sql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mariel_e4
Forum Newbie
Posts: 2
Joined: Sat Apr 07, 2012 8:35 am

please help me i cannot update my sql

Post by mariel_e4 »

I cannot update my sql when i press the save button and make some changes nothing is happening about the data of my rows.What is missing or whats wrong with this i really dont know ive been fixing it for 1 day.and still nothing happened.

Code: Select all

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

$code =$_GET['Code'];
$result = mysql_query("SELECT * FROM books WHERE Code = '$code'");

$row = mysql_fetch_array($result);
if (!$result) 
		{
		die("Error: Data not found..");
		}
				$code =$row['Code'];
				$Title=$row['Title'] ;
				$Price= $row['Price'] ;					
				$Stock=$row['Stock'] ;
				
				
if(isset($_GET['editsave']))
{	
	$title_save = $_GET['title'];
	$price_save = $_GET['price'];
	$stock_save =$_GET['stock'];
	 
	mysql_query("UPDATE books SET Title ='$title_save', Price ='$price_save',
		 Stock='$stock_save'  WHERE Code= '$code'")
				or die(mysql_error()); 
	echo "Saved!";
	
	header("Location: home_admin.php");			
}
?>

<body>
<form method="get">
<table>
	<tr>
		<td>Title:</td>
		<td><input type="text" name="title" value="<?php echo $Title ?>"/></td>
	</tr>
	<tr>
		<td>Price</td>
		<td><input type="text" name="price" value="<?php echo $Price ?>"/></td>
	</tr>
	<tr>
		<td>Stock</td>
		<td><input type="text" name="stock" value="<?php echo $Stock ?>"/></td>
	</tr>
	
	<tr>
		<td>&nbsp;</td>
		<td><input type="submit" name="editsave" value="save" /></td>
	</tr>
</table>

</body>
</html>
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Re: please help me i cannot update my sql

Post by azycraze »

put action for the form

<form action="<?php echo $_SERVER['PHP_SELF']; ?>">
mariel_e4
Forum Newbie
Posts: 2
Joined: Sat Apr 07, 2012 8:35 am

Re: please help me i cannot update my sql

Post by mariel_e4 »

what will I put in action? I dont no what to put and why do i NEEd .I already have a header right? sorry i'm a beginner sorry for stupid questions..
User avatar
azycraze
Forum Commoner
Posts: 56
Joined: Mon Oct 24, 2011 12:08 pm
Location: India

Re: please help me i cannot update my sql

Post by azycraze »

You have to mention what action have to be taken when a submit button in a form is clicked.

Do you get any warnings,notices,errors ?
php_rockzz
Forum Newbie
Posts: 3
Joined: Wed Apr 04, 2012 6:20 am

Re: please help me i cannot update my sql

Post by php_rockzz »

use something like this (untested):

Code: Select all

<?php
include("connect.php");
 
if(_SERVER['REQUEST_METHOD']=='POST')
{
$code =$_GET['Code'];
$result = mysql_query("SELECT * FROM books WHERE Code = '$code'");

$row = mysql_fetch_array($result);
if (!$result)
                {
                die("Error: Data not found..");
                }
                                $code =$row['Code'];
                                $Title=$row['Title'] ;
                                $Price= $row['Price'] ;                                
                                $Stock=$row['Stock'] ;
                               
                               
if(isset($_GET['editsave']))
{      
        $title_save = $_GET['title'];
        $price_save = $_GET['price'];
        $stock_save =$_GET['stock'];
         
        mysql_query("UPDATE books SET Title ='$title_save', Price ='$price_save',
                 Stock='$stock_save'  WHERE Code= '$code'")
                                or die(mysql_error());
        echo "Saved!";
       
        header("Location: home_admin.php");                    
}
}
?>
and the modify the form syntax as:

Code: Select all

<form method="post" action="">
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: please help me i cannot update my sql

Post by phphelpme »

Where does this variable come from?

Code: Select all

$code =$_GET['Code'];
I can not see on your script where this value is coming from and how the value is being passed when you click your button.

You are also missing your closing </form> tag after your ending </table> tag.

Also this:

Code: Select all

$code =$row['Code'];
Seems pointless considering you already have checked to see if this code exists in your database and you already have the value saved as a string variable, but then you grab the exact same code and save again to the exact same string variable name from the database.

Best wishes
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: please help me i cannot update my sql

Post by Celauran »

Also,

Code: Select all

$code = $_GET['code'];
is a pretty bad idea. Sanitize your data!

http://yoursite.com/foo.php?code='%3B%2 ... LE%20books will end in tears.
Last edited by Celauran on Tue Apr 10, 2012 12:46 pm, edited 1 time in total.
phphelpme
Forum Contributor
Posts: 261
Joined: Sun Nov 21, 2010 3:32 pm

Re: please help me i cannot update my sql

Post by phphelpme »

checkout:

mysql_real_escape_string()

htmlspecialchars()

Best wishes
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: please help me i cannot update my sql

Post by Celauran »

Better still, check out PDO.
Post Reply