0

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 from 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>
3
  • Do you have a field name timestamp ? Change that name or use bacticks around it. Commented Aug 30, 2011 at 18:23
  • i want to insert the actual time where the insertion was performed. Is that the right way of declaring a variable to get this? Commented Aug 30, 2011 at 18:32
  • That's fine, yes. The concern was over the name of the field, timestamp - he is concerned that it may be a reserved word. In fact, it is not: dev.mysql.com/doc/refman/5.1/en/reserved-words.html Commented Aug 30, 2011 at 18:34

5 Answers 5

3

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!

Sign up to request clarification or add additional context in comments.

Comments

1

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.

6 Comments

It's possible, but was worth pointing out. Regardless, the variable name is spelled correctly when set, but not when used.
I changed and checked both the variables and the table columns and they are now allo spelled correctly, but that did not solved the issue, I added if($Db->getErrorNum()) { JError::raiseError( 500, $Db->stderr()); } but the page seems to be redirecting before displaing the erro
Are you sure an error is even being generated? Have you verified that the value of proceedButton is not empty? If it is, then none of this database code is being hit.
I just updated the full code, I erased every value and trying to insert a simple string to test.. but nothing gets inserted, meabe I am missing something
That leads me to believe your if statement is failing. Instead of checking for the value of proceedButton, I'd look for the value of checkToProceed. Your JS forces this to be checked before you can continue anyway.
|
1

mysqli_error() or mysql_error().

Comments

1

use mysql_error

1 Comment

Immediately after the query executes, under the line $db->query(). Also, as I note in my answer, you are executing the query twice in your code...
-1

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}');

3 Comments

-1 - Quotes around numeric fields, while possibly not sensible, will not cause an error.
I think I can troubleshoot by using this: if($Db->getErrorNum()) { JError::raiseError( 500, $Db->stderr()); }
But no error is raised , unless is posting to another page, I get redirected to site main page , meabe thats why i dont see error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.