Jump to content

Recommended Posts

any idea why this wont work? im new with update so i have no real idea what im doing here the echo $sql; is a test and everything echos out fine but nothing gets updated into the database  http://www.lawrenceguide.org/user/addhours.php?Id=15 username admin password twisted

 

<?php

$sql = "UPDATE hours  SET Name = '$Name', Id = '$Id', OpenSun = '$OpenSun', CloseSun = '$CloseSun', OpenMon = '$OpenMon', CloseMon = '$CloseMon', OpenTue = '$OpenTue', CloseTue = '$CloseTue', OpenWed = '$OpenWed', CloseWed = '$CloseWed', OpenThur = '$OpenThur', CloseThur = '$CloseThur', OpenFri = '$OpenFri', CloseFri = '$CloseFri', OpenSat = '$OpenSat', CloseSat = '$CloseSat',";

$result = mysql_query($sql); 

 

        // generate and execute query

echo $sql; ?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/125739-solved-error-in-update-insert/
Share on other sites

If you ran the update that you listed (assuming you remove the comma), it will update every row in the table with that information. If there is only one row in the table, and there will only ever have one, then that's OK. But if there's more than one, you need a WHERE clause. It would be structured the same way your SELECT WHERE clauses whould be (for example, "WHERE id = $id" or whatever).

its from a form that submits to the same page does it need a where clause? it just updates the record its displaying

 

The database doesn't know what record you're displaying.  You need the WHERE clause to update the desired record.  Like F1Fan said, if you don't have the WHERE clause it will update every record in that table.

<?php
$Id = mysql_escape_string($_GET['Id']);
$sql = "UPDATE hours WHERE Id = '$Id' SET Name = '$Name', Id = '$Id', OpenSun = '$OpenSun', CloseSun = '$CloseSun', OpenMon = '$OpenMon', CloseMon = '$CloseMon', OpenTue = '$OpenTue', CloseTue = '$CloseTue', OpenWed = '$OpenWed', CloseWed = '$CloseWed', OpenThur = '$OpenThur', CloseThur = '$CloseThur', OpenFri = '$OpenFri', CloseFri = '$CloseFri', OpenSat = '$OpenSat', CloseSat = '$CloseSat'";
$result = mysql_query($sql);  

        // generate and execute query

UPDATE hours WHERE Id = '' SET Name = 'New', Id = '', OpenSun = '1am', CloseSun = '1am', OpenMon = '1am', CloseMon = '1am', OpenTue = '1am', CloseTue = '1am', OpenWed = '1am', CloseWed = '1am', OpenThur = '1am', CloseThur = '1am', OpenFri = '1am', CloseFri = '1am', OpenSat = '1am', CloseSat = '1am'

 

is now displayed as the echo of $sql

yea im not sure why i put it at the begining  i new it needed to be in the end i think im just getting frustrated haha

oh and with this nothing updates

<?php
$Id = mysql_escape_string($_GET['Id']);
$sql = "UPDATE hours SET Name = '$Name', Id = '$Id', OpenSun = '$OpenSun', CloseSun = '$CloseSun', OpenMon = '$OpenMon', CloseMon = '$CloseMon', OpenTue = '$OpenTue', CloseTue = '$CloseTue', OpenWed = '$OpenWed', CloseWed = '$CloseWed', OpenThur = '$OpenThur', CloseThur = '$CloseThur', OpenFri = '$OpenFri', CloseFri = '$CloseFri', OpenSat = '$OpenSat', CloseSat = '$CloseSat' WHERE Id = '$Id'";
$result = mysql_query($sql);  
	echo $sql;
        // generate and execute query

echo of sql

UPDATE hours SET Name = 'test', Id = '', OpenSun = '12am', CloseSun = '1am', OpenMon = '1am', CloseMon = '1am', OpenTue = '1am', CloseTue = '1am', OpenWed = '1am', CloseWed = '1am', OpenThur = '1am', CloseThur = '1am', OpenFri = '1am', CloseFri = '1am', OpenSat = '1am', CloseSat = '1am' WHERE Id = ''Update successful.Go Home.

i see my problem not sure how to fix it tho  look at the echo of sql you see this?

did  i not close the where properly? im lost

WHERE Id = ''Update successful.Go Home.

 

it should be getting the id from the page

$Id = mysql_escape_string($_GET['Id']);

 

ill add error reporting now

solved i changed it up alot tho

 

i ended up going with the form submitting to 

thanks the whole removing the comma and where clause were big but in the end it was basicly submitting without the id attached  this seems to work

now i just gotta fiqure out how to get the selected data  from mysql and get it as the pre selected option of a select box

<?
// includes
include("../template/conf.php");
// open database connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
// select database
mysql_select_db($db) or die ("Unable to select database!");
	$Id = mysql_escape_string($_GET['Id']);
$query = "SELECT * FROM hours WHERE Id = '$Id'";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
echo $query;
    $Name = mysql_escape_string($_POST['Name']);
    $Id = mysql_escape_string($_POST['Id']);
    $OpenSun = mysql_escape_string($_POST['OpenSun']);
    $CloseSun = mysql_escape_string($_POST['CloseSun']);
    $OpenMon = mysql_escape_string($_POST['OpenMon']);
    $CloseMon = mysql_escape_string($_POST['CloseMon']);
    $OpenTue = mysql_escape_string($_POST['OpenTue']);
    $CloseTue = mysql_escape_string($_POST['CloseTue']);
    $OpenWed = mysql_escape_string($_POST['OpenWed']);
    $CloseWed = mysql_escape_string($_POST['CloseWed']);
    $OpenThur = mysql_escape_string($_POST['OpenThur']);
    $CloseThur = mysql_escape_string($_POST['CloseThur']);
    $OpenFri = mysql_escape_string($_POST['OpenFri']);
    $CloseFri = mysql_escape_string($_POST['CloseFri']);
    $OpenSat = mysql_escape_string($_POST['OpenSat']);
    $CloseSat = mysql_escape_string($_POST['CloseSat']);
$sql = "UPDATE hours SET Name = '$Name', Id = '$Id', OpenSun = '$OpenSun', CloseSun = '$CloseSun', OpenMon = '$OpenMon', CloseMon = '$CloseMon', OpenTue = '$OpenTue', CloseTue = '$CloseTue', OpenWed = '$OpenWed', CloseWed = '$CloseWed', OpenThur = '$OpenThur', CloseThur = '$CloseThur', OpenFri = '$OpenFri', CloseFri = '$CloseFri', OpenSat = '$OpenSat', CloseSat = '$CloseSat' WHERE Id = '$Id'";
$result = mysql_query($sql) or die(mysql_error());  
	echo $sql;  
?>

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.