Jump to content

Insert into existing record


TheEddy

Recommended Posts

<?php
if(!isset($_SESSION))
{
session_start();
}
// UNCOMMENT NEXT LINE TO PRINT THE $_SESSION ARRAY TO THE SCREEN . . .
// echo '<pre>'; print_r($_SESSION); echo '</PRE>';
if(empty($_SESSION['userID']) || $_SESSION['authorized'] != true ) {



header("Location: login.php");



exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="EN" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/xml; charset=utf-8" />
<meta http-equiv="refresh" content="5;url=editprofile.php">
    <title>Saving Profile</title>
  </head>

  <body>
<?php
require_once ("dbconn.php");
?>
<?php
$userID = $_SESSION['userID'];
$insert_query = 'insert into users WHERE userID='$userID'(
				aim,
				msn,
				yim,
				psnID,
				xblGamertag,
				otherContact
				) 
				values
				(
				"' . $_POST['aim'] . '", 
				"' . $_POST['msn'] . '",
				"' . $_POST['yim'] . '",
				"' . $_POST['psnID'] . '",
				"' . $_POST['xblGamertag'] . '",
				"' . $_POST['otherContact'] . '"
				)';

mysql_query($insert_query);

?>
Your profile has been saved!  You will now be redirected from where you came from.
<br /><a href="editprofile.php" title="Click here if you don't want to wait">Click here if you don't want to wait.</a>
  </body>
</html>

 

It creates a new record but I want it to update an existing one.  It's an editprofile script.

Link to comment
https://forums.phpfreaks.com/topic/209474-insert-into-existing-record/
Share on other sites

you need to use an update query instead of insert.

<?php
require_once ("dbconn.php");
?>
<?php
$userID = $_SESSION['userID'];
$insert_query = 'UPDATE users 
                 SET aim="' . $_POST['aim'] . '",
			     msn="' . $_POST['msn'] . '",
			     yim="' . $_POST['yim'] . '",
			     psnID="' . $_POST['psnID'] . '",
			     xblGamertag="' . $_POST['xblGamertag'] . '",
			     otherContact="' . $_POST['otherContact'] . '",
			 WHERE userID = $userID;'


mysql_query($insert_query);

?>

 

It says I did something wrong but I can't find it :(

perhaps try...

 

$aim=$_POST['aim'];
$msn = $_POST['msn'];
$yim = $_POST['yim'];
$psnID =$_POST['psnID'];
$xblGamertag = $_POST['xblGamertag'];
$otherContact= $_POST['otherContact'];

$insert_query = "UPDATE users SET aim='$aim, msn='$msn', yim='$yim', psnID='$psnID', xblGamertag='$xblGamertag', otherContact= '$otherContact' WHERE userID = '$userID'";

try implement mysql_error() so you can see the error message if the query isn't functioning. Don't use or die() when the site is live for production, there are much nicer error reporting methods.

i.e.

<?php
require_once ("dbconn.php");
?>
<?php
$userID = $_SESSION['userID'];
$insert_query = 'UPDATE users 
                 SET aim="' . $_POST['aim'] . '",
			     msn="' . $_POST['msn'] . '",
			     yim="' . $_POST['yim'] . '",
			     psnID="' . $_POST['psnID'] . '",
			     xblGamertag="' . $_POST['xblGamertag'] . '",
			     otherContact="' . $_POST['otherContact'] . '",
			 WHERE userID = $userID;'


mysql_query($insert_query) or die(mysql_error());

?>

It says I did something wrong but I can't find it :(

What does it say?

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.