Page 1 of 1

please help me i cannot update my sql

Posted: Sat Apr 07, 2012 8:47 am
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>

Re: please help me i cannot update my sql

Posted: Sat Apr 07, 2012 10:59 am
by azycraze
put action for the form

<form action="<?php echo $_SERVER['PHP_SELF']; ?>">

Re: please help me i cannot update my sql

Posted: Sat Apr 07, 2012 11:18 am
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..

Re: please help me i cannot update my sql

Posted: Sat Apr 07, 2012 3:08 pm
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 ?

Re: please help me i cannot update my sql

Posted: Tue Apr 10, 2012 8:57 am
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="">

Re: please help me i cannot update my sql

Posted: Tue Apr 10, 2012 12:08 pm
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

Re: please help me i cannot update my sql

Posted: Tue Apr 10, 2012 12:38 pm
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.

Re: please help me i cannot update my sql

Posted: Tue Apr 10, 2012 12:43 pm
by phphelpme
checkout:

mysql_real_escape_string()

htmlspecialchars()

Best wishes

Re: please help me i cannot update my sql

Posted: Tue Apr 10, 2012 12:45 pm
by Celauran
Better still, check out PDO.