updating data using php

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
chino
Forum Newbie
Posts: 7
Joined: Thu Mar 26, 2015 11:02 am

updating data using php

Post by chino »

Hello guys, I have created a form where users can edit their post, everything works correctly but when the user clicks on the "edit" button it doesn't update. It display "Query was empty" any idea where I have gone wrong? Thank in advance.

Code: Select all


<?php

$connect = mysql_connect("igor.gold.ac.uk","ma301sl","fitnesspal1") or die ("couldn't connect!");
mysql_select_db("ma301sl_blog") or die("couldn't find db");


if(!isset($_POST['submit'])) {
    
   $query = "SELECT * FROM post WHERE id=$id";
    $results = mysql_query($query);
    $row = mysql_fetch_array($result);
       
                            }
?>

<h1>You are Modifying...</h1>
<form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        The title of the post<input type="text" name="inputTitle" value="<?php echo $row['title']; ?>" />
        Creator<input type="text" name="inputCreator" value="<?php echo $row['creator']; ?>" />
     Body of the post<textarea name="inputText" value="<?php echo $row['body']; ?>"> </textarea>
        
        <input type="hidden" name="id" value="<?php $_GET['id']; ?>" />
        <input type="submit" name="submit" value="Edit" />
    </form>

<?php
   if(isset($_POST['submit'])) {
       
    $update = "UPDATE post SET `title`='$_POST[inputTitle]', `creator`='$_POST[inputCreator]' WHERE ID= $_POST[id]";  
      mysql_query($query) or die (mysql_error());
       
       echo "user has been midified";
       header("Location: page.php");
     
         
   } 
   
 ?>

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

Re: updating data using php

Post by Celauran »

Code: Select all

    $update = "UPDATE post SET `title`='$_POST[inputTitle]', `creator`='$_POST[inputCreator]' WHERE ID= $_POST[id]";  
      mysql_query($query) or die (mysql_error());
You're defining the query as $update, then trying to execute $query.

You're also using deprecated mysql_* functions, aren't validating or sanitizing your inputs, aren't using prepared statements, etc.
chino
Forum Newbie
Posts: 7
Joined: Thu Mar 26, 2015 11:02 am

Re: updating data using php

Post by chino »

It updates now but I also get an error message. please help me why i'm getting this? Thanks in advance.
DB Error, could not query the database MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Code: Select all



<?php

$connect = mysql_connect("","","") or die ("couldn't connect!");
mysql_select_db("ma301sl_blog") or die("couldn't find db");


if(isset($_GET['pid'])) {
    
   
        $pid = $_GET['pid'];
        $res = mysql_query(" SELECT * FROM post WHERE id=$pid ");
        $row = mysql_fetch_array($res);
       
                            }

if( isset($_POST['newTitle']) )
	{
		$newTitle = $_POST['newTitle'];
		$id  	 = $_POST['id'];
		$sql     = "UPDATE post SET title='$newTitle' WHERE id='$id'";
		$res 	 = mysql_query($sql) or die("Could not update".mysql_error());
		echo "<meta http-equiv='refresh' content='0;url=page.php'>";
	}
    
?>

<h1>You are Modifying...</h1>
<form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
        The title of the post :
        <br />
        <input  type="text" name="newTitle" value="<?php echo $row[1]; ?>" style="width:10%;" />
        <br> Creator :
        <br />
        <input  type="text" name="newCreator" value="<?php echo $row[2]; ?>" style="width:10%;" />
        <br> body of the post :
        <br />
        <input  type="text" name="newBody" value="<?php echo $row[3]; ?>" style="width:90%;" />
    
        
        <input type="hidden" name="id" value="<?php echo $row[0]; ?>" />
        <br />
        <br />
        <input type="submit" value="update" />
    </form>








User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: updating data using php

Post by Christopher »

Does the SELECT or UPDATE give the error?

PS - You need to filter/validate you input. Please use the mysqli extension. The mysql extension is no longer supported.
(#10850)
chino
Forum Newbie
Posts: 7
Joined: Thu Mar 26, 2015 11:02 am

Re: updating data using php

Post by chino »

the error is in the update

Code: Select all



if( isset($_POST['newTitle']) )
        {
                $newTitle = $_POST['newTitle'];
                $id      = $_POST['id'];
                $sql     = "UPDATE post SET title='$newTitle' WHERE id='$id'";
                $res     = mysql_query($sql) or die("Could not update".mysql_error());
                echo "<meta http-equiv='refresh' content='0;url=page.php'>";
        }

DB Error, could not query the database MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: updating data using php

Post by Celauran »

You're not checking that $_POST['id'] contains any value. Have you echoed the parsed query? What does that look like?
chino
Forum Newbie
Posts: 7
Joined: Thu Mar 26, 2015 11:02 am

Re: updating data using php

Post by chino »

it updates all my tables but I don't understand why I also get the error, sorry I'm a big noob with php. maybe I can surround it with brackets {}?
Post Reply