7 Replies - 2486 Views - Last Post: 24 September 2012 - 03:38 PM Rate Topic: -----

#1 rjclancy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 18-March 11

insert into two different tables using php

Posted 22 September 2012 - 08:35 AM

Hi guys...im trying to figure out how i can input data into 2 table's using just 1 form
i have two tables, polls and poll_options.
I need to insert into polls 'poll_id(auto encrament)', 'title', 'user_id')

then i need to insert into poll_options ('poll_id(which i have to recieve from the polls table)', 'postan1', 'postan2', 'postan3')

Here is my form

<form action="newPoll.php" method="POST">
		Tilte:<input  type="text" name="title"/><br />
		Answer 1:<input  type="text" name="postan1"/><br />
		Answer 2:<input  type="text" name="postan2"/><br />
		Answer 3:<input  type="text" name="postan3"/><br />
		<input type="hidden" name="user_id" value="<?php echo $_SESSION['user_id'];?>"/>
		<input type="submit" value="submit">
</form>



and here is my newPoll.php(which might look ugly)
<?php include'init.php';?>
<?php

	
	$postTitle=$_POST['title'];
	$user_id=$_POST['user_id'];
	$postanswer1=$_POST['postan1'];
	$postanswer2=$_POST['postan2'];
	$postanswer3=$_POST['postan3'];
	

	$sql = "INSERT INTO `polls` (title, user_id) VALUES ('$postTitle','$user_id')";
	
	$result = mysql_query($sql);
	if(!$result)
	{
		echo("unable to input poll");
	}
	else
	{
		echo("input succeded");
	}
	
header('Location:profile.php');
?>



any help would be great....the first insert is working but i dont know were to go after that...

This post has been edited by rjclancy: 22 September 2012 - 08:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: insert into two different tables using php

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: insert into two different tables using php

Posted 22 September 2012 - 08:53 AM

This is always wrong:
$x = $_POST['x'];

Ok, it's not technically wrong. Let's just call it horrible code.

Now go look in the tutorials section for Dormilich's tutorial on PDO and repost your code using prepared statements!
Was This Post Helpful? 0
  • +
  • -

#3 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: insert into two different tables using php

Posted 22 September 2012 - 09:05 AM

Quick writeup, between new PDO you should put the right values and the poloptions you should change to your needs
<?php
	$postTitle=$_POST['title'];
	$user_id=$_POST['user_id'];
	$postanswer1=$_POST['postan1'];
	$postanswer2=$_POST['postan2'];
	$postanswer3=$_POST['postan3'];
	
	$db = new PDO(''); // change it with the right details
	$sql = "INSERT INTO `polls` (title, user_id) VALUES (?,?)";
	
	$polStmt = $db->prepare($sql);
	$polStmt->bindValue(1, $postTitle);
	$polStmt->bindValue(2, $user_id);
	$polStmt->execute();
	if($polStmt->execute())
	{
		$id = $db->lastInsertId();
		$sql = "INSERT INTO poll_options(id, col1) VALUES(?,?)";
		
		$polOptStmt = $db->prepare($sql);
		$polOptStmt->bindValue(1, $id);
		$polOptStmt->bindValue(2, $postanswer1);
		$polOptStmt->execute();
	}
	else
	{
		echo("unable to input poll");
	}
	
header('Location:profile.php');
exit();
?>

Was This Post Helpful? 1
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: insert into two different tables using php

Posted 22 September 2012 - 09:25 AM

Just to be clear, this:
    $polStmt->bindValue(1, $postTitle);

could easily be this:
    $polStmt->bindValue(1, $_POST['title']);


meaning that there is no need for this waste of processor cycles:
    $postTitle=$_POST['title'];


Was This Post Helpful? 0
  • +
  • -

#5 nightscream  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 237
  • Joined: 04-December 08

Re: insert into two different tables using php

Posted 22 September 2012 - 09:32 AM

yes of course, it's faster and better if you don't do any functions on the $_POST like trim or htmentities, ...
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: insert into two different tables using php

Posted 22 September 2012 - 09:35 AM

Right. It's the copy-only that burns me up. Not only does it waste resources but it makes the code longer. Longer code is more likely to be harder to read/edit/debug.
Was This Post Helpful? 0
  • +
  • -

#7 rjclancy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 18-March 11

Re: insert into two different tables using php

Posted 22 September 2012 - 09:47 AM

View Postnightscream, on 22 September 2012 - 09:05 AM, said:

Quick writeup, between new PDO you should put the right values and the poloptions you should change to your needs
<?php
	$postTitle=$_POST['title'];
	$user_id=$_POST['user_id'];
	$postanswer1=$_POST['postan1'];
	$postanswer2=$_POST['postan2'];
	$postanswer3=$_POST['postan3'];
	
	$db = new PDO(''); // change it with the right details
	$sql = "INSERT INTO `polls` (title, user_id) VALUES (?,?)";
	
	$polStmt = $db->prepare($sql);
	$polStmt->bindValue(1, $postTitle);
	$polStmt->bindValue(2, $user_id);
	$polStmt->execute();
	if($polStmt->execute())
	{
		$id = $db->lastInsertId();
		$sql = "INSERT INTO poll_options(id, col1) VALUES(?,?)";
		
		$polOptStmt = $db->prepare($sql);
		$polOptStmt->bindValue(1, $id);
		$polOptStmt->bindValue(2, $postanswer1);
		$polOptStmt->execute();
	}
	else
	{
		echo("unable to input poll");
	}
	
header('Location:profile.php');
exit();
?>


Hi nightscream... i never even heard of this PDO...ill be looking into it and ill get back to you...BIG BIG thanks for the post:)
Was This Post Helpful? 0
  • +
  • -

#8 rjclancy  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 18-March 11

Re: insert into two different tables using php

Posted 24 September 2012 - 03:38 PM

View Postnightscream, on 22 September 2012 - 09:05 AM, said:

Quick writeup, between new PDO you should put the right values and the poloptions you should change to your needs
<?php
	$postTitle=$_POST['title'];
	$user_id=$_POST['user_id'];
	$postanswer1=$_POST['postan1'];
	$postanswer2=$_POST['postan2'];
	$postanswer3=$_POST['postan3'];
	
	$db = new PDO(''); // change it with the right details
	$sql = "INSERT INTO `polls` (title, user_id) VALUES (?,?)";
	
	$polStmt = $db->prepare($sql);
	$polStmt->bindValue(1, $postTitle);
	$polStmt->bindValue(2, $user_id);
	$polStmt->execute();
	if($polStmt->execute())
	{
		$id = $db->lastInsertId();
		$sql = "INSERT INTO poll_options(id, col1) VALUES(?,?)";
		
		$polOptStmt = $db->prepare($sql);
		$polOptStmt->bindValue(1, $id);
		$polOptStmt->bindValue(2, $postanswer1);
		$polOptStmt->execute();
	}
	else
	{
		echo("unable to input poll");
	}
	
header('Location:profile.php');
exit();
?>


Hi nightscream....iv been trying to get this working, and i seem to be getting somewere, but i think i might have a problem...all my code is mysql and not mysqli...i read somewere online to use PDO my code has to be mysqli????would this seem right to you???

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1