...

View Full Version : Data not updating in database.



martynball
04-03-2010, 05:09 PM
<?php
include "../../scripts/connect.php";

//Load users from dataabse
mysql_select_db($dbname, $con);
$query="SELECT * FROM users";
$result=mysql_query($query);
if (!$result) {
$mess = "Unable to update details! (User check error!)";
header("Location:../login.php?mess=$mess");
}

//Check if username and password matchs
while ($row=mysql_fetch_array($result)) {
$username=$_SESSION['username'];
$password=$_SESSION['password'];

if ($uname != $username && $pword != $password) {
$_SESSION['username'] = "NULL";
$_SESSION['password'] = "NULL";
$mess="Login details are incorrect, unable to update information!";
header("Location: ../login.php?mess=$mess");
}
}

//Unset variables for reuse.
unset($query,$result);

//Get data from form
$var1=$_POST['1'];
$var2=$_POST['2'];
$var3=$_POST['3'];
$var4=$_POST['4'];
$var5=$_POST['5'];
$var6=$_POST['6'];
$var7=$_POST['7'];
$var8=$_POST['8'];

//Update database with new information
mysql_select_db($dbname, $con);
$query="UPDATE tablename SET
col1=".$var1.",
col2=".$var2.",
col3=".$var3.",
col4=".$var4.",
col5=".$var5.",
col6=".$var6.",
col7=".$var7.",
col8=".$var8." WHERE id=".$id;

//Add the data
$result=mysql_query($query);
mysql_close($con);


//Check that the data has been updated
if (!result) {
$mess = "Error updating information!";
header("Location: ../index.php?mess=$mess");
} else {
$mess = "Successfully updated information!";
header("Location: ../index.php?mess=$mess");
} ?>


Var names and column name have been changed due to client wants topic to be private.

Question: The data which is put into the form is not updating in the database, although, the correct data is being displayed if I print the form data onto the screen. :S

abduraooft
04-03-2010, 05:23 PM
//Add the data
$result=mysql_query($query);
I'm tired of saying this in CF :(. You need to add proper error checks to your query. Change the above to
//Add the data

$result=mysql_query($query) or die(mysql_error(). '<br>'. $query); and check the result.

martynball
04-03-2010, 05:28 PM
Cheers, got it working now.

Fix:


$query="UPDATE tablename SET
col1='$var1',
col2='$var2'....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum