$ _POST PHP UPDATE错误而不是发送选项
问题描述:
I am trying $_POST an UPDATE to the following mysql table:
Note: there are 3 dropdowns: status, category and access
app_generalData
---------------
app_id
table
status_id
category_id
tags
access_id
Resulting tests with print_r($_POST) and echo(s) 's ensure the form $_POST is working:
Array (
[MAX_FILE_SIZE] => 100000
[app_id] => 1
[title] => Nothing Special
[status] =>
[category] =>
[tags] => new tag
[access] =>
[update] => Update )
Data from the form.
- ID: 1
- Title: Nothing Special
- Status:
- Category:
- Tags: new tag
- Access:
Resulting Error message:
Error querying database for General Data.
Desired result(s):
- $_POST items updating in the db
Snippet for UPDATE:
// Post the UPDATE to app_generalData
if (isset($_POST['update'])) {
// print_r($_POST);
// echo '<br />';
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Grab the data from the POST
// General Data
$app_id = mysqli_real_escape_string($dbc, trim($_POST['app_id']));
$title = mysqli_real_escape_string($dbc, trim($_POST['title']));
$status = mysqli_real_escape_string($dbc, trim($_POST['status']));
$category = mysqli_real_escape_string($dbc, trim($_POST['category']));
$tags = mysqli_real_escape_string($dbc, trim($_POST['tags']));
$access = mysqli_real_escape_string($dbc, trim($_POST['access']));
// Confirm success with the user
echo '<h1>Data from the form.</h1><br />';
echo 'ID: ' . $app_id .'<br />';
echo 'Title: ' . $title .'<br />';
echo 'Status: ' . $status .'<br />';
echo 'Category: ' . $category .'<br />';
echo 'Tags: ' . $tags .'<br />';
echo 'Access: ' . $access .'<br />';
echo '<br />';
// Write the data to the database
$query = "UPDATE app_generalData
SET title = $title,
status_id = $status,
category_id = $category,
tags = $tags,
access_id = $access
WHERE app_id = $app_id
";
mysqli_query($dbc,$query)
or die('Error querying database for General Data.');
// close MySQL
mysqli_close($dbc);
exit();
}
else {
echo 'Please enter all details below.';
}
答
Whenever you get mysql_errors
i'd suggest the first thing you do if the problem is not found by just looking at the code, is echoing out the query.
What seems to be your problem here is you are not surrounding your string in ''
(quotes) like:
$query = "UPDATE app_generalData
SET title = '$title',
status_id = '$status',
category_id = '$category',
tags = '$tags',
access_id = '$access'
WHERE app_id = '$app_id'
";
答
After running a print_r($query)
, I was able to notice that the query was posting the following:
UPDATE app_generalData
SET title = Nothing Special,
status_id = ,
category_id = ,
tags = new tag,
access_id =
WHERE app_id = 1
Reason for error: there are no marks (" or ') around the values?
Working solution:
I've updated the query to the following
UPDATE app_generalData
SET title = '".$title."',
status_id = '".$status."',
category_id = '".$category."',
tags = '".$tags."',
access_id = '".$access."'
WHERE app_id = $app_id
";