php - Simple SQL Update Statement Syntax -
attempting update record within mysql database php. php noob i'm assuming i'm missing obvious.
my new code (update-task.php) - submits , redirects record not updated.
<?php $data = array( 'id' => $_post['id'], 'taskname' => $_post['taskname'], 'clientname' => $_post['clientname'], 'assignedto' => $_post['assignedto'], 'duedate' => $_post['duedate'], 'timelogged' => $_post['timelogged'], 'notes' => $_post['notes'], 'urgent' => $_post['urgent'] ); require('mysql.php'); $db = new mysql; $db->connect('localhost', 'my_user', 'my_pass;', 'my_db'); $db->query("update `my_table` set taskname='" . mysql_real_escape_string($data['taskname']) . "', clientname='" . mysql_real_escape_string($data['clientname']) . "', assignedto='" . mysql_real_escape_string($data['assignedto']) . "', duedate='" . mysql_real_escape_string($data['duedate']) . "', timelogged='" . mysql_real_escape_string($data['timelogged']) . "', notes='" . mysql_real_escape_string($data['notes']) . "', urgent='" . mysql_real_escape_string($data['urgent']) . "' id='" . mysql_real_escape_string($data['id']) . "' "); header('location:saved.htm');
my form looks (view-task.php)
<?php $id = $_get['id']; require('mysql2.php'); $db = new mysql; $db->connect('localhost', 'my_user', 'my_pass;', 'my_db'); $query = $db->query("select * `table_name` id = '" . $id ."'"); while($result = $db->fetch_array($query)) { $data = $result; } ?> <form action="update-task.php" method="post" id="form1" name="form1"> <label>task name:</label> <input name="taskname" type="text" id="taskname" size="30" value="<?php echo $data['taskname']?>"/> <label>client</label> <input name="clientname" type="text" id="clientname" size="30" value="<?php echo $data['clientname']?>"/> <label>assigned to</label> <input name="assignedto" type="text" id="assignedto" size="30" value="<?php echo $data['assignedto']?>"/> <label>due date</label> <input name="duedate" type="text" id="duedate" size="30" value="<?php echo $data['duedate']?>"/> <label>time logged</label> <input type="button" value="start count" onclick="timedcount()"> <input type="text" value="<?php echo $data['timelogged']?>" id="timelogged" name="timelogged"> <input type="button" value="stop count" onclick="stopcount()"> <input type="button" value="clear timer" onclick="cleartimer()"> <label>notes</label> <input name="notes" type="textarea" id="notes" size="30" value="<?php echo $data['notes']?>"/> <label>urgent</label> <input name="urgent" type="text" id="urgent" size="30" value="<?php echo $data['urgent']?>"/> <input type="submit" value="update" class="addbutton"> </form>
contents of mysql.php
<?php class mysql { var $querynum = 0; var $querylist = ''; function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect=0) { $die = false; if($pconnect) { @mysql_pconnect($dbhost, $dbuser, $dbpw) or ($die = true); } else { @mysql_connect($dbhost, $dbuser, $dbpw) or ($die = true); } if($die) { die(mysql_error()); } mysql_select_db($dbname) or die(mysql_error()); } function fetch_array($query, $type=mysql_assoc) { $query = mysql_fetch_array($query, $type); return $query; } function query($sql) { $query = mysql_query($sql) or die(mysql_error()); $this->querynum++; $this->querylist .= "$sql <br />"; return $query; } function result($query, $row) { $query = mysql_result($query, $row); return $query; } function num_rows($query) { $query = mysql_num_rows($query); return $query; } function insert_id() { $id = mysql_insert_id(); return $id; } function fetch_row($query) { $query = mysql_fetch_row($query); return $query; } function affected_rows() { $query = mysql_affected_rows(); return $query; } function close() { mysql_close(); } } ?>
if updating data, have uniq id, post code mysql.php mysql update query works "update
my_tableset id='1'"
. looks have query error
<?php $data = array( 'id' => $_post['id'] 'taskname' => $_post['taskname'], 'clientname' => $_post['clientname'], 'assignedto' => $_post['assignedto'], 'duedate' => $_post['duedate'], 'timelogged' => $_post['timelogged'], 'notes' => $_post['notes'], 'urgent' => $_post['urgent'] ); require('mysql.php'); $db = new mysql(); $db->connect('localhost', 'my_user', 'my_pass;', 'my_db'); $db->query("update `my_table` set taskname='" . mysql_real_escape_string($data['taskname']) . "', clientname='" . mysql_real_escape_string($data['clientname']) . "', assignedto='" . mysql_real_escape_string($data['assignedto']) . "', duedate='" . mysql_real_escape_string($data['duedate']) . "', timelogged='" . mysql_real_escape_string($data['timelogged']) . "', notes='" . mysql_real_escape_string($data['notes']) . "', urgent='" . mysql_real_escape_string($data['urgent']) . "' id='" . mysql_real_escape_string($data['id']) . "' )"); header('location:saved.htm'); ?>
form
<?php $data['id'] = $_get['id']; require('mysql.php'); $db = new mysql(); $db->connect('localhost', 'my_user', 'my_pass;', 'my_db'); $query = $db->query("select * `my_table` id = '" . $data['id'] ."'"); while($result = $db->fetch_array($query)) { $data = array_merge($data, $result); } ?> <form action="update-task.php" method="post"> <label>task name:</label> <input name="taskname" type="text" id="taskname" size="30" value="<?= $data['taskname']?>"/> <label>client</label> <input name="clientname" type="text" id="clientname" size="30" value="<?= $data['clientname']?>"/> <label>assigned to</label> <input name="assignedto" type="text" id="assignedto" size="30" value="<?= $data['assignedto']?>"/> <label>due date</label> <input name="duedate" type="text" id="duedate" size="30" value="<?= $data['duedate']?>"/> <input name="id" type="hidden" id="id" value="<?= $data['id']?>"/> <label>time logged</label> <input type="button" value="start count" onclick="timedcount()"> <input type="text" value="<?= $data['timelogged']?>" id="timelogged" name="timelogged"> <input type="button" value="stop count" onclick="stopcount()"> <input type="button" value="clear timer" onclick="cleartimer()"> <label>notes</label> <input name="notes" type="textarea" id="notes" size="30" value="<?= $data['notes']?>"/> <label>urgent</label> <input name="urgent" type="text" id="urgent" size="30" value="<?= $data['urgent']?>"/> <input type="submit" value="update" class="addbutton"> </form>
Comments
Post a Comment