Jump to content

Form updates table row


Russia

Recommended Posts

I would like to make this script so that instead of inserting info it updates the code to update the first row.

 

I thought of adding this to the update

WHERE id='$id'

and having this to say what row

$id = $rows['id'];

So something like this:

 

<?php
require('inc/config.php');
$id = $rows['id'];
$sql="UPDATE Persons WHERE id='$id' (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
?> 

And the form:

 

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

</body>
</html> 

Is that correct?

 

and mostly will it work?

Link to comment
https://forums.phpfreaks.com/topic/180310-form-updates-table-row/
Share on other sites

if you had tried it, you'd have figured out that it does not work.

 

you are using UPDATE incorrectly:

 

<?php
$sql = "UPDATE `table` SET `field1` = '".$field1."', `field2` = '".$field2."' WHERE `somefield` = '".$somevariable."'";
?>

 

where is $rows being set?  will that value be coming from another database query?

 

EDIT: and always, always scrub/sanitize your incoming variables with mysql_real_escape_string() before running them through the query.

 

UPDATE syntax

 

usage of mysql_real_escape_string() would be:

 

<?php
$firstname = mysql_real_escape_string ($_POST['firstname']);
//and so on for each incoming variable going to the db;
?>

Edit:

 

Okay I have done that, will it work?

<?php
include ('inc/config.php');
$id = $rows['id'];  

mysql_query("UPDATE Persons WHERE id='$id' SET
$FirstName = mysql_real_escape_string ($_POST['firstname']);
$LastName = mysql_real_escape_string ($_POST['lastname']);
$MiddleName= mysql_real_escape_string ($_POST['middlename']);
");
?>  

Is that correct? and will it work?

So it will be like this?

 

<?php
include ('inc/config.php');
$id = $rows['id'];  

mysql_query("UPDATE Persons WHERE id='$id' SET
FirstName = '$_POST[firstname]',
LastName = '$_POST[lastname]',
MiddleName = '$_POST[middlename]'
");

?>

 

Is this correct?

 

match your query to mine, and it becomes quite obvious that it's not.

 

your WHERE clause is in the wrong spot, and you haven't used mysql_real_escape_string() on your variables.  and wrap your variables in the query with curly braces '{$firstname}' if you are going to do it that way.

 

EDIT: do you have a platform you can test these queries on?  please tell me you're not just being lazy, 'cause to be testing these out will actually allow you to learn from your mistakes .. unless this is just to pass a class or something, in which case, my time is better spent elsewhere.

Like this?

 

<?php
include ('inc/config.php');
$id = $rows['id'];  

$sql = "UPDATE `persons` WHERE id='$id' SET 
`FirstName` = '".$firstname."', 
`LastName` = '".$lastname."',
`LastName` = '".$middlename."'";

?>

 

or

 

<?php
include ('inc/config.php');
$id = $rows['id'];  

$sql = "UPDATE `persons` WHERE id='$id' SET 
`FirstName` = '".$firstname."', 
`LastName` = '".$lastname."',
`LastName` = '".$middlename."' 
WHERE id='$id'";

?>

I am getting no errors.

is $id an integer or string?  if it's an integer, single quotes should not be used in the query, otherwise, as seen below is fine.

 

try this:

 

<?php
$sql = mysql_query ("
UPDATE
	`persons`
SET 
	`FirstName` = '".$firstname."', 
	`LastName` = '".$lastname."',
	`LastName` = '".$middlename."' 
WHERE
	`id` = '".$id."'
");
?>

Okay I am using it like this:

 

<?php
include ('inc/config.php');
$id = $rows['id'];  

$sql = mysql_query ("
   UPDATE
      `persons`
   SET 
      `FirstName` = '".$firstname."', 
      `LastName` = '".$lastname."',
      `LastName` = '".$middlename."' 
   WHERE
      `id` = '".$id."'
"); 

?>

 

Is that correct?

you really, really need to pay attention to everything that somebody tells you.

 

for example, where have you set your variables $firstname, $middlename, $lastname?

 

you're trying to jump to the conclusion so quickly that the means to get to the end just aren't lining up.

 

i showed you to declare your variables as such:

 

<?php
$firstname = mysql_real_escape_string ($_POST['firstname']);

//and so on...
?>

 

otherwise, the query will not have anything to use.

 

and, add:

 

 OR die (mysql_error());

 

to the end of the query, replacing the last semi-colon with the code above.

 

and, are you sure you are setting $id with a value?  what's going on inside config.php?

Okay so like this?

 

<?php
include ('inc/config.php');
$firstname = mysql_real_escape_string ($_POST['firstname']);
$lastname = mysql_real_escape_string ($_POST['lastname']);
$middlename= mysql_real_escape_string ($_POST['middlename']);

$id = $rows['id'];  
$sql = mysql_query ("
   UPDATE
      `persons`
   SET 
      `FirstName` = '".$firstname."', 
      `LastName` = '".$lastname."',
      `LastName` = '".$middlename."' 
   WHERE
      `id` = '".$id."'
"); 
OR die (mysql_error());
?>

<?php
include ('inc/config.php');
$firstname = mysql_real_escape_string ($_POST['firstname']);
$lastname = mysql_real_escape_string ($_POST['lastname']);
$middlename= mysql_real_escape_string ($_POST['middlename']);

$id = 2;  
$sql = mysql_query ("
   UPDATE
      `testing`
   SET 
      `FirstName` = '".$firstname."', 
      `LastName` = '".$lastname."',
      `MiddleName` = '".$middlename."' 
   WHERE
      `id` = '".$id."'
")
OR die (mysql_error());
?>

 

Fixed so there's no error

 

 

Right now it is editing row 2. But in my column I only have 1 row and will have only 1 row, so how would I do it so it automatically edits the only row there do I use the * thing?

just remove the WHERE clause.

 

part of becoming a developer is giving in to your curiosity, meaning, exercise those "what if" questions that run through your head.  "What if" i remove the WHERE clause .. what happens?

 

so, just take out:

 

WHERE `id` = '".$id."'

 

from your query.

 

NOTE: if you ever had more than one record in the db, doing the above will overwrite ALL records .. you can add:

 

LIMIT 1

 

to the end of the query to ensure only one record is updated.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.