0

I'm having a few issues with MySQLi queries. I have read the docs for PHP several times and have encountered the same error. I am new to MySQLi but have used MySQL.

Here is the error I am receiving after submitting the post data:

[22-Mar-2014 23:41:17 UTC] PHP Fatal error: Call to a member function bind_param() on a non-object in /home/ponypwna/public_html/Changelist/cpanel.php on line 32

Here is my code for overviewing:

<?php
$MysqlUsername = "*****";
$MysqlPassword = "*****";
$MysqlHostname = "localhost";
$MysqlDatabase = "ponypwna_mane";
/* Establishing Connection here */
$mysqli = new mysqli($MysqlHostname, $MysqlUsername, $MysqlPassword, $MysqlDatabase) or die("Mysql Error: " . $mysqli->error);

//Did we post it?
if (isset($_POST['insertChange'])) {
    #Fetching Post Data
    $change = $_POST['change'];
    $state = $_POST['state'];
    $appliesto = $_POST['appliesto'];
    $progress = $_POST['progress'];
    $completiondate = $_POST['completiondate'];
    $contributor = $_POST['contributor'];

    #Preparing Query
    $insertChange = $mysqli->prepare("INSERT INTO changelist (change, state, appliesto, progress, completiondate, contributor) VALUES (?, ?, ?, ?, ?, ?)");
    $insertChange->bind_param('sssiss', $change, $state, $appliesto, $progress, $completiondate, $contributor);

    #Executing Prepared Query
    $insertChange->execute();

    #Close statement and function
    $insertChange->close();
}
?>
  • 2
    you're not setting `$mysqli` anywhere that I can see... – erik258 Mar 22 '14 at 23:44
  • Is `$mysqli` variable ever initialized? – barell Mar 22 '14 at 23:44
  • @barell I have set it but for security reasons, I did not include in the post. – LordNature Mar 22 '14 at 23:45
  • Add it to the code please, but do not include your passwords. There is also another spelling error with your code `$insertChange-close();` there should be `->` not `-` – barell Mar 22 '14 at 23:47
  • The error message implies that either `$mysqli` is not initialised, or the initialisation has failed. Check the code where you're opening the connection and make sure you have some error checking that will tell you what is happening –  Mar 22 '14 at 23:47
  • Is the $mysqli object created in a function/other class and so its out of scope for this code ? – Dave Mar 22 '14 at 23:48
  • @MikeW Thanks for that comment. I mistyped up my MySQLi connection, but I did fix it before your post. I then got another error about bind_params. If you could examine a bit closer that would be helpful. – LordNature Mar 23 '14 at 00:01
  • @LordNature removed my post. Value binding seems right on second view :) ... – dognose Mar 23 '14 at 00:24
  • @LordNature: is your "date string" a valid date format or maybe a localized one? – dognose Mar 23 '14 at 00:28
  • @dognose Not sure- My method of getting this data is through a form; this form contains an input with type "date". Would this work or is it invalid for SQL? – LordNature Mar 23 '14 at 00:30

2 Answers2

0

New Answer
It seems the error is being caused due to an error in your sql syntax.

When you do:

$insertChange = $mysqli->prepare("INSERT INTO changelist (change, state, appliesto, progress, completiondate, contributor) VALUES (?, ?, ?, ?, ?, ?)");

and when here is an error in the syntax, $insertChange is set to false and so it has no method called bind_param() as per the documentation here

Return Values
mysqli_prepare() returns a statement object or FALSE if an error occurred.


So a fix would be to copy-past the sql into an phpMyAdmin or whatever and replace the ? with actual data and run it to see if it works. Maybe one of your columns are missing, spelling error?
Krimson
  • 7,386
  • 11
  • 60
  • 97
  • as of the comments: He has instantiated mysqli, just not included it in the code. So the remaining (correct) part of your answer is just a copy of mine :P – dognose Mar 22 '14 at 23:54
  • @Krimson I actually added in the definition, but if you read the post. I explain that I did not add it for security reasons. I added the code though for people who passed that up. – LordNature Mar 22 '14 at 23:55
  • @dognose Excuse me but when I typed up my answer, he didn't have the mysqli initiation... Check the edited section for your self. And no I didn't copy your answer for the last part. its something i noticed my self. Please stop assuming thing. Downvote is only when an answer is not helpfull. Not when you feel competitive and vulnerable – Krimson Mar 22 '14 at 23:57
  • @Krimson: He mentioned in the comments 3 Minutes before you submitted your post. I'm not feeling competitive. Im just sick of copy/pasting existing answers and/or comments in order to get a "+1". However if you edit your post, i'll remove the "-1". Just can't without another edit. – dognose Mar 22 '14 at 23:59
  • @dognose He may have not seen it, but thanks for both your answers. They were helpful. – LordNature Mar 23 '14 at 00:02
  • @LordNature I have updated my answer, check that out – Krimson Mar 23 '14 at 00:12
  • @Krimson Either we are on the "same" level, or you are f*** kidding me :P (See my comment(s) on my post) :P – dognose Mar 23 '14 at 00:13
  • @dognose I say we are on the same level. – Krimson Mar 23 '14 at 00:14
  • @Krimson Could accept that :P – dognose Mar 23 '14 at 00:14
  • @Krimson Click [here](http://prntscr.com/338s5l) for my table structure. – LordNature Mar 23 '14 at 00:19
  • Upon second look, I seem to be receiving this error from MySQL (after adding a few debugging tools I was able to see this 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 'change, state, appliesto, progress, completiondate, contributor) VALUES (?, ?, ?' at line 1 – LordNature Mar 23 '14 at 00:26
0

We are all dumb :)

Upon second look, I seem to be receiving this error from MySQL (after adding a few debugging tools I was able to see this 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 'change, state, appliesto, progress, completiondate, contributor) VALUES (?, ?, ?' at line 1

"change" is a reserved keyword in MYSQL. https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Add `` arround change (it is a good idea to wrap every column name - there are various reserved keywords):

$insertChange = $mysqli->prepare("INSERT INTO changelist (`change`, state, appliesto, progress, completiondate, contributor) VALUES (?, ?, ?, ?, ?, ?)");
dognose
  • 20,360
  • 9
  • 61
  • 107