brokekidweb
brokekidweb

Reputation: 157

Unable to input numbers into MySQL using php statement

I am working on a website where an administrator can edit a schedule that they already created. They can click on any item on the schedule to edit it. For example, they can click on the shift start time and then it directs them to a page where they can update the value.

Unfortunately, I have not been able to get this to work for every value. It seems to be that the text values are working just fine, but I am getting a syntax error when it is a number.

Here is what I am using to update:

$type = $_GET['type'];
$value = $_GET['value'];
$week = $_GET['week'];
$newval = $_POST['newval'];
if(strlen($newval) > 0)
{
    include '../dbinfo.php';
    $type = $mysqli->real_escape_string($_POST['type']);
    $week = $mysqli->real_escape_string($_POST['week']);
    $tablename = $mysqli->real_escape_string("cs" . $_SESSION['squadron']);
    $newval = $mysqli->real_escape_string($newval);
    if((is_numeric($newval)))
    {
        $sql = "UPDATE $tablename SET $type=$newval WHERE week=$week";
    }
    else
    {
        $sql = "UPDATE $tablename SET $type='$newval' WHERE week=$week";
    }
    if($result = $mysqli->query($sql))
    {
        echo "Your specififed changed was completed successfully!<br>";
        echo "<a href='edit.php?week=" . $week . "'>Continue editing</a>";
    }
    else
    {
        echo mysqli_error($result);
    }
}

Changing a string results in the sql statement:

UPDATE cs14 SET shift_1_name='Test' WHERE week=1 (this works)

Changing a number results in the sql statement:

UPDATE cs14 SET shift_ 1_starttime=940 WHERE week=1 (this doesn't work)

It is giving me the MySQL error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1_starttime=940 WHERE week=1' at line 1

I have already researched this error, and I have checked the syntax over and over again. It doesn't work in phpmyadmin either. I have no idea what to check next!

Can anyone help me out with my syntax here??? Thanks!

Upvotes: 0

Views: 111

Answers (2)

believe me
believe me

Reputation: 908

At the numeric update query put quotes around,

$sql = "UPDATE $tablename SET $type='$newval' WHERE week='$week'";

Upvotes: 1

Husman
Husman

Reputation: 6909

The $type variable contains a space. Remove the space from it.

More specifically "shift_ 1_starttime" contains a space. Wherever your setting $type to "shift_ 1_starttime" remove the space from it. Or if thats how it is defined in the database surround it with backticks `

$sql = "UPDATE $tablename SET `$type`='$newval' WHERE week=$week";

Upvotes: 0

Related Questions