Jump to content

Cant Update ;S


jerry89

Recommended Posts

Hi, I have problem with UPDATE, when I press update, it just refresh the page, and wont update it.

I cant see whats wrong :(

<?php require_once("../includes/connection.php"); ?>


<?php 
// FORM PROCCESS
if (isset($_POST['submit'])) {



$query = "UPDATE `meta` SET `keys` = '$keys' WHERE keys = '$keys' LIMIT 1 ";


$result = mysql_query($query);
	if (mysql_affected_rows() == 1) {
		$message = "Update succesfull";
		} else {
		$message = "There is problem";
		$message = "<br />" .mysql_error();
		}
		}
// END OF PROCCESS
?>


<?php

$sql = "SELECT * FROM meta";
$result = MYSQL_QUERY($sql);
$numberOfRows = mysql_num_rows($result);
	if ($numberOfRows==0) {  
   			echo "Sorry. No records found !!"; 
	}
	else  {
   			$row = mysql_fetch_array($result);
   
   			$keys = $row['keys'];
		}
?>

<FORM action="meta.php" method="post">
<p>Edit:<br />
<textarea name="content" rows="10" cols="40"><?php echo $keys; ?></textarea>
</p>


<input type="submit" name="submit" value="Update Page" />
    <br />
<?php echo $message; ?>
</FORM>

 

Hope you can help me

Link to comment
Share on other sites

You never define $keys, you are trying to update the table where keys = $keys and you are updating keys to $keys, which makes no sense.

So how do I do it, that it updates the database from the text area ?

 

I just looked on the php admin and try update from there.

 

so its gives me this

 

UPDATE `test` SET `keys` = 'test' WHERE CONVERT( `test`.`keys` USING utf8 ) = 'test' LIMIT 1 ;

 

and when I try it , it wont update after I try do in browser :(

Link to comment
Share on other sites

First you need to know what row you want to update. You need a unique identifier (a Primary Key is preferred) for the table.

 

So what is your table structure? You will need to pass that with the form.

 

Second, your textarea name is "content" not $keys, so you need to use $_POST['content'] to reference the new value of the text area.

Link to comment
Share on other sites

First you need to know what row you want to update. You need a unique identifier (a Primary Key is preferred) for the table.

 

So what is your table structure? You will need to pass that with the form.

 

Second, your textarea name is "content" not $keys, so you need to use $_POST['content'] to reference the new value of the text area.

 

The table structure is just database name , and one table " test=> keys" thats all .. so if I put keys as primary key.

 

and where include the $_post['content'] :(

 

I am very sorry , I am beginer in php

Link to comment
Share on other sites

Learn proper SQL first. If you ask me.

 

A primary key should generally be an auto_incrementing integer, this is to prevent confusion with 2 of the same keys and allow you to update the proper records. Because without your update querying will not run, unless you pass the original keys each time, which is sort of ludicrous.

 

Here would be a working example of your script as is.

 

<?php 
require_once("../includes/connection.php");
   // FORM PROCCESS
   if (isset($_POST['submit'])) {   
         $query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE keys = '{$_POST['keys']}' LIMIT 1 ";
      
         $result = mysql_query($query);
         if (mysql_affected_rows() == 1) {
            $message = "Update succesfull";
         } else {
            $message = "There is problem";
            $message .= "<br />" .mysql_error(); // needed a .= for appending
         }
    }
   // END OF PROCCESS

   $sql = "SELECT * FROM meta";
   $result = MYSQL_QUERY($sql);
   $numberOfRows = mysql_num_rows($result);
      if ($numberOfRows==0) {  
            echo "Sorry. No records found !!"; 
      }
      else  {
            $row = mysql_fetch_array($result);
   
            $keys = $row['keys'];
       }
?>
   <FORM action="meta.php" method="post">
   <p>Edit:<br />
   <input type="hidden" name="keys" value="<?php echo $keys; ?>" />
   <textarea name="content" rows="10" cols="40"><?php echo $keys; ?></textarea>
   </p>

   <input type="submit" name="submit" value="Update Page" />
    <br />
<?php echo $message; ?>
   </FORM>

Link to comment
Share on other sites

Learn proper SQL first. If you ask me.

 

A primary key should generally be an auto_incrementing integer, this is to prevent confusion with 2 of the same keys and allow you to update the proper records. Because without your update querying will not run, unless you pass the original keys each time, which is sort of ludicrous.

 

Here would be a working example of your script as is.

 

<?php 
require_once("../includes/connection.php");
   // FORM PROCCESS
   if (isset($_POST['submit'])) {   
         $query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE keys = '{$_POST['keys']}' LIMIT 1 ";
      
         $result = mysql_query($query);
         if (mysql_affected_rows() == 1) {
            $message = "Update succesfull";
         } else {
            $message = "There is problem";
            $message .= "<br />" .mysql_error(); // needed a .= for appending
         }
    }
   // END OF PROCCESS

   $sql = "SELECT * FROM meta";
   $result = MYSQL_QUERY($sql);
   $numberOfRows = mysql_num_rows($result);
      if ($numberOfRows==0) {  
            echo "Sorry. No records found !!"; 
      }
      else  {
            $row = mysql_fetch_array($result);
   
            $keys = $row['keys'];
       }
?>
   <FORM action="meta.php" method="post">
   <p>Edit:<br />
   <input type="hidden" name="keys" value="<?php echo $keys; ?>" />
   <textarea name="content" rows="10" cols="40"><?php echo $keys; ?></textarea>
   </p>

   <input type="submit" name="submit" value="Update Page" />
    <br />
<?php echo $message; ?>
   </FORM>

 

 

I will look at some SQL tutorials.

 

I tryed the way you send it , says "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 'keys = 'test' LIMIT 1' at line 1"

 

Em do I need to put the keys as "auto_incremt with primary key"

 

I am very sorry

Link to comment
Share on other sites

Not sure where/why that is failing (other than maybe keys is a reserved word).

 

$query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE `keys` = '{$_POST['keys']}' LIMIT 1 ";

 

Try that and see if it fixes the error.

 

Given this statement:

Em do I need to put the keys as "auto_incremt with primary key"

 

Learn SQL.

 

EDIT:

Once you get the basics down you should learn Normalization of SQL (3rd Normal Form).

 

Here is description about auto_incrementing

Link to comment
Share on other sites

Not sure where/why that is failing (other than maybe keys is a reserved word).

 

$query = "UPDATE `meta` SET `keys` = '{$_POST['content']}' WHERE `keys` = '{$_POST['keys']}' LIMIT 1 ";

 

Try that and see if it fixes the error.

 

Given this statement:

Em do I need to put the keys as "auto_incremt with primary key"

 

Learn SQL.

 

EDIT:

Once you get the basics down you should learn Normalization of SQL (3rd Normal Form).

 

Here is description about auto_incrementing

 

It works ;)

 

Thank you so much

Thank you so 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.