Jump to content

[SOLVED] Update records using form


joshgarrod

Recommended Posts

Hi, I just need a second pair of eyes to go over this script for me, It pulls the correct information for the record I want to edit. It just doesn't update, it says it has but it hasn't. I have this code working on another page but I have manipulated it for this but it doesn't work for some reason... any ideas?  Thanks

 

<?php
error_reporting (E_ALL ^ E_NOTICE);
$idir = "uploads/";   // Path To Images Directory
$usr = "user";
    $pwd = "pass";
    $db = "quest";
    $host = "host";

/***********************Upload File***********************/
if (isset ($_FILES['fupload']))
{
//upload the image to tmp directory
$url = $_FILES['fupload']['name'];   // Set $url To Equal The Filename For Later Use 
if ($_FILES['fupload']['type'] == "image/jpg" || $_FILES['fupload']['type'] == "image/jpeg" || $_FILES['fupload']['type'] == "image/pjpeg")
{ 
	$file_ext = strrchr($_FILES['fupload']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php 
	$copy = copy($_FILES['fupload']['tmp_name'], "$idir" . $_FILES['fupload']['name']);   // Move Image From Temporary Location To Permanent Location 
}
}

/***********************Get Record***********************/
$ref = (!empty($_GET['ref']))?trim($_GET['ref']):"";

# connect to database
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid){
echo("ERROR: " . mysql_error() . "\n");
}
$db_selected = mysql_select_db('quest', $cid);

$query = sprintf("SELECT * FROM shows where ref like '%s' LIMIT 1","%".mysql_real_escape_string($ref)."%");
$result = mysql_query($query) or die($query."<br>".mysql_error());  
$record = mysql_fetch_assoc($result);
if(mysql_num_rows($result) > 0)
{
echo "FOUND";
$ref=$record["ref"];
$title=$record["title"];
$descr=$record["descr"];
$location=$record["location"];
$startdate=$record["startdate"];
$enddate=$record["enddate"];
}else{
echo "NOT FOUND";
$ref=0;
$title="";
$descr="";
$location="";
$startdate="";
$enddate="";
}


/***********************Save Record***********************/
# this is processed when the form is submitted
# back on to this page (POST METHOD)
if (isset($_POST['submit'])){
$ref=$_POST["ref"];
$title=$_POST["title"];
$descr=$_POST["descr"];
$location=$_POST["location"];
$startdate=$_POST["startdate"];
$enddate=$_POST["enddate"];

# setup SQL statement
$query = sprintf("UPDATE `shows` SET `title` = '%s',`descr` = '%s',`location` = '%s',`startdate` = '%s',`enddate` = '%s' WHERE `shows`.`ref` ='%s' LIMIT 1",	mysql_real_escape_string($title),mysql_real_escape_string($descr),mysql_real_escape_string($location),mysql_real_escape_string($startdate),mysql_real_escape_string($enddate),mysql_real_escape_string($ref));

#execute SQL statement
$result = mysql_db_query($db,$query,$cid) or die($query."<br>".mysql_error());

# check for error
if (!$result){
	echo("ERROR: " . mysql_error() . "\n$SQL\n");
}
echo "<P>Part updated</P>\n";
}
   
?>

Link to comment
Share on other sites

Echo the update query to the page. Chances are the values aren't getting set correctly. My guess is that if you are getting the message "Part updated" and no errors are displayed then the $ref value isn't getting set. Since you didn't provide the form, I can't tell whether there is even such a field on the form.

 

Try adding the following lines after the confirmation message to validate teh post values and the query. Is it what you expect?

echo "Query: <br />{$query}\n";
echo "Post Values: <br /><pre>";
print_r($_POST);
echo "</pre>";

Link to comment
Share on other sites

Edit: Some duplicate of the above ^^^^ post...

 

First of all, only use E_ALL in the following (hiding Notice messages, the ^ E_NOTICE part, is probably hiding errors that would help pin point why your code is not working) -

 

error_reporting (E_ALL ^ E_NOTICE);

 

Echo out $query so that you know what it contains. Since it appears that the query is being executed, that would mean that your WHERE clause does not match any rows in your table or your table has more than one row and the LIMIT 1 is causing only one to be updated while you are examining a different one.

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.