11 Replies - 261 Views - Last Post: 14 March 2014 - 12:23 PM Rate Topic: -----

#1 NewtoPHP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 17-February 14

PHP MYSQL insert with session variables

Posted 14 March 2014 - 06:04 AM

Hi

Im having a bit of trouble with my sql insert statement. Can someone give me any tips on where i am going wrong?


session_start(); 


$con=mysql_connect("localhost","######","#####","~~~~~");
// Check connection
if (mysql_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysql_connect_error();
  }

$sqla = mysql_query("SELECT Customer_ID FROM customer WHERE Email_Address = '$_SESSION[Email_Address]'")'");
 
 
$row = mysql_fetch_array($sqla);

 
 
  if ($row > 0)  {
  
		
$sql = mysql_query($con,"INSERT INTO booking (Customer_ID, customer_type, Date_P, Date_R, Time_P, Time_R, Location1) VALUES('".$row['Customer_ID']."',255,'20140507','20140507','160000','160000')");		

	 echo "Success";
}	
  

mysql_close($con);



When I try and insert values without the select statement it works fine. And the select statement does echo out the customer_id with the email address session. However just not sure how i insert the result of the query into the value section of the insert statement.

Any tips would be grateful.

Thanks :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: PHP MYSQL insert with session variables

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3333
  • View blog
  • Posts: 11,296
  • Joined: 12-December 12

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 06:24 AM

You have specified 7 fields but only have 6 values.
Was This Post Helpful? 0
  • +
  • -

#3 NewtoPHP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 17-February 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 06:28 AM

View Postandrewsw, on 14 March 2014 - 06:24 AM, said:

You have specified 7 fields but only have 6 values.



Sorry location one isnt meant to be there. I have 6 fields and 6 values and it still doesnt work

Thanks for pointing that out though cheers. Can you see anything else wrong with it?
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 09:27 AM

Well! The problem seems to be in your mysql_query() function...
The function takes the form,
mysql_query(query,connection)



Whereas you pass the opposite arguments. Also, the function is deprecated. You can use MySQLi instead...

Are those typos in the queries? Or you messed up concatenating the variable in the query... Also check for affected rows by the query...

Hope it Helps!
Was This Post Helpful? 2
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 09:44 AM

Is there a reason you're using deprecated mysql functions?
Was This Post Helpful? 0
  • +
  • -

#6 NewtoPHP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 17-February 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 11:14 AM

View PostCTphpnwb, on 14 March 2014 - 09:44 AM, said:

Is there a reason you're using deprecated mysql functions?



changing it now! cheers for pointing that out
Was This Post Helpful? 0
  • +
  • -

#7 NewtoPHP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 17-February 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 11:20 AM

View PostNewtoPHP, on 14 March 2014 - 11:14 AM, said:

View PostCTphpnwb, on 14 March 2014 - 09:44 AM, said:

Is there a reason you're using deprecated mysql functions?



changing it now! cheers for pointing that out


sorry I have changed it all to msqli and it says cannot connect to database
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 1,300
  • Joined: 15-January 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 11:22 AM

Make sure you're using it correctly:

http://www.php.net/m...i.construct.php
Was This Post Helpful? 1
  • +
  • -

#9 NewtoPHP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 17-February 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 11:31 AM

View PostArtificialSoldier, on 14 March 2014 - 11:22 AM, said:

Make sure you're using it correctly:

http://www.php.net/m...i.construct.php


Seem to be getting this error now

mysqli_query() expects at least 2 parameters, 1 given

and

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given
Was This Post Helpful? 0
  • +
  • -

#10 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 1,300
  • Joined: 15-January 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 11:38 AM

Look up the manual pages for mysqli_query and mysqli_fetch_assoc. If you have questions about how to use the functions then look them up. Look at the error message and go straight to the manual, cut out the middle man. Look at the parameters that mysqli_query expects. Look at the parameters that mysqli_fetch_assoc expects. Look at the examples on the manual.
Was This Post Helpful? 1
  • +
  • -

#11 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2913
  • View blog
  • Posts: 10,088
  • Joined: 08-August 08

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 11:48 AM

Don't simply change to mysqli. Be sure that you're properly using prepared statements or your code will be just as vulnerable as with mysql.
Was This Post Helpful? 1
  • +
  • -

#12 NewtoPHP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 17-February 14

Re: PHP MYSQL insert with session variables

Posted 14 March 2014 - 12:23 PM

View PostArtificialSoldier, on 14 March 2014 - 11:38 AM, said:

Look up the manual pages for mysqli_query and mysqli_fetch_assoc. If you have questions about how to use the functions then look them up. Look at the error message and go straight to the manual, cut out the middle man. Look at the parameters that mysqli_query expects. Look at the parameters that mysqli_fetch_assoc expects. Look at the examples on the manual.



Thanks a lot! I eventually got it working got and got help from looking at those php manual pages :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1