Jump to content

update database


garydt

Recommended Posts

When a logged-in user inputs new information I'm trying to insert the information into a field of the same record as their username. At the moment it's adding it to new record. How can I correct this please?

 

 

<?php require_once('Connections/elvisdb.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

session_start();

$user = $_SESSION['MM_Username'];

 

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

 

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

  $insertSQL = sprintf("INSERT INTO userinformation (why) VALUES (%s)",

                      GetSQLValueString($_POST['textfield'], "text"));

 

 

  $sql = "UPDATE userinformation SET

          colWHY = '$textfieldWHY',

          WHERE usernm = '$user' ";

mysql_query ($sql);

 

  mysql_select_db($database_elvisdb, $elvisdb);

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());

 

  $insertGoTo = "usernmm.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

 

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">

  <label>

  <input type="text" name="textfield" />

  </label>

  <p>

    <label>

    <input type="submit" name="Submit" value="Submit" />

    </label>

  </p>

  <input type="hidden" name="MM_insert" value="form1">

</form>

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/40156-update-database/
Share on other sites

Thats because your using the INSERT statement which will always create a new record

 

Look up the UPDATE statement.. Here's a tutorial:-

 

http://www.w3schools.com/php/php_mysql_update.asp

 

And here's the syntax for the UPDATE syntax:-

 

UPDATE table SET column='value' WHERE column='value'

Link to comment
https://forums.phpfreaks.com/topic/40156-update-database/#findComment-194296
Share on other sites

i revised my code and i get this error-

 

Parse error: parse error, unexpected '}' in C:\Program Files\xampp\htdocs\epeople\hi.php on line 56

 

my code-

 

<?php require_once('Connections/elvisdb.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

 

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

 

  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;   

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  return $theValue;

}

}

session_start();

$user = $_SESSION['MM_Username'];

 

$editFormAction = $_SERVER['PHP_SELF'];

if (isset($_SERVER['QUERY_STRING'])) {

  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);

}

 

 

  mysql_select_db($database_elvisdb, $elvisdb); 

  $sql = "UPDATE userinformation SET

          colWHY = '$textfieldWHY',

          WHERE usernm = '$user' ";

 

 

 

  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());

 

  $insertGoTo = "usernmm.php";

  if (isset($_SERVER['QUERY_STRING'])) {

    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";

    $insertGoTo .= $_SERVER['QUERY_STRING'];

  }

  header(sprintf("Location: %s", $insertGoTo));

}

 

 

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>

 

<body>

<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">

  <label>

  <input type="text" name="textfield" />

  </label>

  <p>

    <label>

    <input type="submit" name="Submit" value="Submit" />

    </label>

  </p>

  <input type="hidden" name="MM_insert" value="form1">

</form>

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/40156-update-database/#findComment-194345
Share on other sites

i took out  the last } and got this error-

 

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\Program Files\xampp\htdocs\epeople\hi.php:2) in C:\Program Files\xampp\htdocs\epeople\hi.php on line 32

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\Program Files\xampp\htdocs\epeople\hi.php:2) in C:\Program Files\xampp\htdocs\epeople\hi.php on line 32

Query was empty

 

Link to comment
https://forums.phpfreaks.com/topic/40156-update-database/#findComment-194362
Share on other sites

Try this:

 

<?php 
ob_start();
require_once('Connections/elvisdb.php'); 

?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
session_start();
$user = $_SESSION['MM_Username'];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


  mysql_select_db($database_elvisdb, $elvisdb);  
  $sql = "UPDATE userinformation SET
          colWHY = '$textfieldWHY',
          WHERE usernm = '$user' ";



  $Result1 = mysql_query($insertSQL, $elvisdb) or die(mysql_error());

  $insertGoTo = "usernmm.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
ob_flush;


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
  <label>
  <input type="text" name="textfield" />
  </label>
  <p>
    <label>
    <input type="submit" name="Submit" value="Submit" />
    </label>
  </p>
  <input type="hidden" name="MM_insert" value="form1">
</form>
</body>

</html>

 

Tell me if this works or what errors you get

Link to comment
https://forums.phpfreaks.com/topic/40156-update-database/#findComment-194368
Share on other sites

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.