Mysql Query Syntax Problems
Can someone tell me what is wrong on my sql query sintax?
Also is there any way on mysql to get the exact error of the query?
<?php
// If the constant _JEXEC is not defined, quit now.
// This stops this script f开发者_如何学编程rom running outside of the system.
defined( '_JEXEC' ) or die( 'Restricted access' );
?>
<script type="text/javascript" language="javascript">
function proceed()
{
check = document.getElementById('checkToProceed');
proceedButton = document.getElementById('proceedButton');
if (check.checked)
{
proceedButton.disabled = false;
}
else
{
proceedButton.disabled = true;
}
}
</script>
<?php
if ($_POST['proceedButton'] != '') {
$user = JFactory::getUser();
$id = $user->get('id');
$name = $user->get('name');
$username = $user->get('username');
$department = $user->get('department');
$vardate = date("m/d/y : H:i:s", time());
$acknowledge = 1;
$courseTitle = $mainframe->getPageTitle();
/***************************************/
$db = &JFactory::getDBO();
$query = "INSERT INTO `jos_jquarks_users_acknowledge`(course_name)VALUES('hello')";
$db->setQuery($query);
$db->query();
echo mysql_error ();
if($db->getErrorNum()) {
JError::raiseError( 500, $db->stderr());
}
}
?>
<form name="quiz_info" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" id="checkToProceed" name="checkToProceed" onclick="proceed();" />
<label for="checkToProceed">
<?php echo JText::_('I have Read and Acknowledge the procedure'); ?>
</label>
<input id="proceedButton" name="proceedButton" disabled="true" value="Acknowledge" type="submit" />
<input type="hidden" name="layout" value="default" /> <?php echo JHTML::_( 'form.token' ); ?>
</form>
mysql_error
(docs) will return the last error from the database.
It looks like your code will execute the query twice, by the way. Only one call to $db->query()
should be used.
As far as what, if anything is wrong, that's difficult to say. I do not see any apparent errors, but I do wonder about escaping the values you're using in the query. See the docs for mysql_real_escape_string - if any of those values contain an apostrophe, your query would be broken.
I also see a spelling error, "acknoledge" is spelled "acknowledge", so if the field is spelled correctly in the database, then there will be a problem with the misspelling in the query. On that same note, some of your fields use underscores_names
where some use camelCase
names. You're just begging for a typo like this. Use one naming convention or the other, and keep it consistent. If your field is named employeeNumber
and you use employee_number
or EmployeeNumber
, the query will fail. Pick a naming convention and stick to it so you aren't guessing every time you write a query!
I think it could be a couple of things, but there's a spelling error with "acknowledge". It's spelled incorrectly in the column list of the inserts, as well as the variable you use inside the INSERT
statement. It's spelled correctly when you assign that variable.
Edit: Worked through this in chat with OP. The problem was that this code was being used as a snippet inside a Joomla article. The form's post had to go to $_SERVER['REQUEST_URI']
to post back to the proper place.
mysqli_error() or mysql_error().
use mysql_error
Without the error and table structure is a bit hard but...
Maybe you're trying to insert an int datatype as a string, for example user_id. Remove the '' when using numeric values, because they are not strings.
For example:
INSERT INTO table_name
(id_field, string_field)
VALUES
({$id_value}, '{$string_value}');
精彩评论