6 Replies - 318 Views - Last Post: 30 March 2014 - 04:35 PM Rate Topic: -----

#1 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

PHP mysqli statement now not running?

Posted 13 March 2014 - 03:13 AM

I re-uploaded some code to my server today, and discovered that it was not working. Now this is strange because the EXACT SAME SCRIPT was working before I re-uploaded it. I was attempting to make a change in which the newlines from the form were parsed. I have spent the last 3 hours trying to find a reason why this code won't now work, and am totally unsucessful so far.

Can anyone spot an issue?
NOTE: I have replaced some things with x's for obvious reasons...but they aren't wrong, as that is identical in a working script. Also, the change I made (which should be okay) is on line 14. Addition of the nl2br().

Code:
<?php
$mysqli = new mysqli('localhost', 'xxx', 'xxx', 'xxx');

   if(mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
   }
   
	//variables
	$fnm = $_POST["fnameemail"];
	$lnm = $_POST["lnameemail"];
	$eml = $_POST["addemail"];
	$msgsbj = $_POST["msgsubject"];
	$msgcont = nl2br($_POST["messageemail"]);
   
   if ($stmt = $mysqli->prepare("INSERT INTO tablename(id, fname, lname, qrymail, subject, message, timesub, reponded, whoresp, timerep, respmsg) VALUES(null, ?, ?, ?, ?, ?, NOW(), 'N', '', '', '')")) {
   
   /* Bind parameters
         s - string, b - blob, i - int, etc */
      $stmt->bind_param("sssss", $fnm, $lnm, $eml, $msgsbj, $msgcont);
	
	/* Execute it */
    $stmt->execute();

	/* Close statement */
    $stmt->close();
   }

   /* Close connection */
   $mysqli->close();
	
	header("Location:thankyou.php");
?>


Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: PHP mysqli statement now not running?

#2 Slice  Icon User is offline

  • sudo pacman -S moneyz


Reputation: 244
  • View blog
  • Posts: 717
  • Joined: 24-November 08

Re: PHP mysqli statement now not running?

Posted 13 March 2014 - 04:50 AM

Have you checked the permissions of the file?

If you're working on the file on a Windows machine, sometimes the permissions get all screwed.

If it's not that, are you getting any error messages?
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 704
  • View blog
  • Posts: 3,018
  • Joined: 03-December 12

Re: PHP mysqli statement now not running?

Posted 13 March 2014 - 04:54 AM

What version are you using?
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 398
  • View blog
  • Posts: 1,455
  • Joined: 15-January 14

Re: PHP mysqli statement now not running?

Posted 13 March 2014 - 09:30 AM

When you say the code won't work, what exactly do you mean? What happens when you run it? Does the record get added to the database?

One thing to add would be to check if the execute method fails and returns false, and if so then show the error message.
Was This Post Helpful? 0
  • +
  • -

#5 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: PHP mysqli statement now not running?

Posted 13 March 2014 - 04:43 PM

Absolutely nothing happens. There is no entry added, and the script then goes straight to the header location.

The PHP Version is 5.3.27
The Apache Version is 2.2.26

And yes, I have made sure that the permissions are correct.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 398
  • View blog
  • Posts: 1,455
  • Joined: 15-January 14

Re: PHP mysqli statement now not running?

Posted 13 March 2014 - 04:50 PM

It sounds like there is an error with the query. Check the return value of the execute method and print the error message if necessary. Make sure to exit if you do that before redirecting.

http://www.php.net/m...-stmt.error.php
Was This Post Helpful? 0
  • +
  • -

#7 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: PHP mysqli statement now not running?

Posted 30 March 2014 - 04:35 PM

I have solved it!
After way to many hours of blind stumbling, I figured out where the issue was.

What I did, is I removed the
header("Location:thankyou.php");
, then I added print lines after each section - one when if statement true and starts, one when parameters binded, one when stmt executed, etc.
Then I also added the following at line 27
else{print("IF STATEMENT FAILED");}

When I ran the code, would you believe it! I got the message "IF STATEMENT FAILED"...so the prepared statement was failing, which explains why I wasn't getting any errors like ArtificialSoldier suggested.

To solve the issue, I removed some of the values from the prepared statement, and hallelujah! It worked :)


For those coming across this topic, the tweaks made to the code I have put below.

<?php
$mysqli = new mysqli('localhost', 'xxx', 'xxx', 'xxx');

   if(mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
   }
   
	//variables
	$fnm = $_POST["fnameemail"];
	$lnm = $_POST["lnameemail"];
	$eml = $_POST["addemail"];
	$msgsbj = $_POST["msgsubject"];
	$msgcont = nl2br($_POST["messageemail"]);
   
   if ($stmt = $mysqli->prepare("INSERT INTO tablename(id, fname, lname, qrymail, subject, message, timesub, reponded) VALUES(null, ?, ?, ?, ?, ?, NOW(), 'N')")) {
   
   /* Bind parameters
         s - string, b - blob, i - int, etc */
      $stmt->bind_param("sssss", $fnm, $lnm, $eml, $msgsbj, $msgcont);
	
	/* Execute it */
    $stmt->execute();

	/* Close statement */
    $stmt->close();
   }

   /* Close connection */
   $mysqli->close();
	
	header("Location:thankyou.php");
?>


In short, I removed the variables which weren't being used in this statement, so they will default to null.

Thanks for all your replies!
Regards,
-kosmro-
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1