Ashley Sheridan;10982721 wrote:
If you echo out your query, you'll see stuff like:
'UPDATE eating SET break = 'Array[break]', m_tea ='
In future, if you need to use array elements in a string like this, wrap the variable in curly braces like so:
$sql = "UPDATE tablename SET field1='{$safe['field1']}'";
One last thing to note, you shouldn't try to access array elements like this:
$array[element];
Not necessarily true. Usually, $array[element] will indeed issue a warning, since element is a non-defined constant for which PHP issues a warning and then assumes should take the value 'element'.
However, in string interpolation, you can use either "{$array['element']}" or "$array[element]"
$_POST['break'] = '09:00';
$query = "UPDATE eating SET break = '$_POST[break]'";
echo $query;
which outputs
UPDATE eating SET break = '09:00'
which is indeed a perfectly valid SQL query (allthough you'd most likely want WHERE id=...).
As for the initial post. If you want help, I suggest you wrap php code in php tags, html code in html tags and other code in code tags, and also indendt it properly. I only took a quick glance at it, since it's too much of an effort to read unformatted code, but you should always need to check if a query is successful or not. Do this first, and you may just find your problem yourself.
From the php manual at [man]mysqli_query[/man]
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.
Your code
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
which means $data is either a result set or boolean false. If it's false, mysqli_fetch_array produces an error since [man]mysqli_fetch_array[/man]
Procedural style
mixed mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )
does not take a boolean value as its first argument. What you should do
if (!$data = mysqli_query(...))
{
/* trigger_error will "create" a PHP error/warning which is dealt with like any
* other PHP error. If php.ini is set to display them, it will be displayed.
* If php.ini is set to log them, they will be logged. In a production
* environment, they should be logged, not displayed. In a development
* environment, they should be logged and/or displayed, whatever you prefer
*/
# So, use one of ...
trigger_error();
error_log();
# or other handling if possible. Perhaps you can recover from the error?
# perhaps you need to inform the user that they couldn't save data etc
/* If the error was critical, such as a failed login atttempt,
* exit/die may be what you want, but you never ever want
* to die('some kind of internal error message');
* since that gives a potential hacker/griefer information about your system that
* they should not have. Something like
* die('An error occurred. If the problem persists, contact ...');
* would be far better.
* And personally, I prefer showing a normal page, with navigation, styling etc
* but by replacing any content with an error message
*/
}