Jump to content


2 insterts from one


13 replies to this topic

#1 _*Creative Insanity_*

  • Guests

Posted 30 August 2007 - 08:29 PM

Is it possible to insert into 2 tables from one form?

The second table is all predefined hidden data and I was hoping that an auto insert after the main insert happened.

This is the auto insert. I have this under the main insert. But blanks the page out.

// insert into actions
$cat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];

mysql_query("INSERT INTO `actions` (`act_cat`,`action`,`datee`) VALUES ('$cat','$action','$datee'")or die(mysql_error());

ta muchly

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 30 August 2007 - 09:04 PM

Well, you can either simply construct a second query to insert whatever into the second table, or you can look into using database functions. I've read some things in the MySQL manual about using functions or procedures that automatically execute when the table is acted upon; for example, I remember a comment on making it so when a table has data INSERT'ed to it, it automatically updates another table with stuff like time and the row ID.

#3 _*Creative Insanity_*

  • Guests

Posted 30 August 2007 - 09:36 PM

Oh Ok.. I thought that's what I was doing with my insert. Might have to play more with it's placement I guess.

#4 stingerblue

    Young Padawan

  • Members
  • Pip
  • 16 posts
  • Gender:Male
  • Location:North of the Border, UK

Posted 30 August 2007 - 10:06 PM

It should be possible.

$cat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];

$first = mysql_query("INSERT INTO `first table` (`act_cat`,`action`,`datee`) VALUES ('$cat','$action','$datee'")or die(mysql_error());

$second = mysql_query("INSERT INTO `second table` (`act_cat`,`action`,`datee`) VALUES ('$cat','$action','$datee'")or die(mysql_error());

if(($first)&&($second))
  {

echo("worked");

} else {

echo("error");

}

Edited by stingerblue, 30 August 2007 - 10:07 PM.


#5 _*Creative Insanity_*

  • Guests

Posted 31 August 2007 - 03:39 AM

Hey thanks Stinger .. great help. I will place then in my script files. Nice!

#6 stingerblue

    Young Padawan

  • Members
  • Pip
  • 16 posts
  • Gender:Male
  • Location:North of the Border, UK

Posted 31 August 2007 - 07:15 AM

View PostCreative Insanity, on Aug 31 2007, 09:39 AM, said:

Hey thanks Stinger .. great help. I will place then in my script files. Nice!
You're welcome <3.

#7 _*Creative Insanity_*

  • Guests

Posted 31 August 2007 - 09:33 AM

Oh dear..

if generated this error:

Quote

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 '' at line 1
Line one is only the include for the connection script that I use all the time so that is ok.
This is the code I used.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_track")) {
$acat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];
$tname = $_POST['name'];
$tfile = $_POST['file'];
$img = $_POST['img'];
$genre = $_POST['genre'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];

$first = mysql_query("INSERT INTO `music` (`name`,`file`,`img`,`genre`,`txt`,`cat`) VALUES ('$tname','$tfile','$img','$genre','$txt','$cat',")or die(mysql_error());

$second = mysql_query("INSERT INTO `actions` (`act_cat`,`action`,`datee`) VALUES ('$acat','$action','$datee'")or die(mysql_error());
  
  $insertGoTo = "track_upload.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

Edited by Creative Insanity, 31 August 2007 - 09:34 AM.


#8 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 31 August 2007 - 04:26 PM

INSERT INTO `music` (`name`,`file`,`img`,`genre`,`txt`,`cat`) VALUES ('$tname','$tfile','$img','$genre','$txt','$cat',

Notice the comma and absence of the closing parenthesis for the VALUES statement.

Also note that when an error is returned by MySQL, it is referring to the lines of the SQL statement; so in this case, anywhere is pretty much line 1 since you have the full query on one line in every query. MySQL doesn't know its interacting with PHP, and PHP is simply returning the error given from MySQL, which is the error in the command MySQL received from PHP. :)

#9 _*Creative Insanity_*

  • Guests

Posted 31 August 2007 - 09:05 PM

Oh shucks *face turns red* hehe Ta Demonslay

#10 _*Creative Insanity_*

  • Guests

Posted 31 August 2007 - 09:31 PM

Still didn't work.. bugger.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_track")) {
$acat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];
$tname = $_POST['name'];
$tfile = $_POST['file'];
$img = $_POST['img'];
$genre = $_POST['genre'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];

$first = mysql_query("INSERT INTO `music` (`name`,`file`,`img`,`genre`,`txt`,`cat`) VALUES ('$tname','$tfile','$img','$genre','$txt','$cat'")or die(mysql_error());

$second = mysql_query("INSERT INTO `actions` (`act_cat`,`action`,`datee`) VALUES ('$acat','$action','$datee'")or die(mysql_error());
  
  $insertGoTo = "track_upload.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

Edited by Creative Insanity, 31 August 2007 - 09:31 PM.


#11 stingerblue

    Young Padawan

  • Members
  • Pip
  • 16 posts
  • Gender:Male
  • Location:North of the Border, UK

Posted 01 September 2007 - 10:17 AM

View PostCreative Insanity, on Sep 1 2007, 03:31 AM, said:

Still didn't work.. bugger.

What was the error? Plus the if(($first)&&($second)) is missed out, that bit is to check that both inserts were successful.

May I ask what this bit of code is for, as it only looks like a header redirect to me:

$insertGoTo = "track_upload.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

*Edit: Some of the inserts have wrong names, like $act_cat: $acat = $_POST['act_cat']; the name used to insert it into the database would be $acat.

---

None of the inserts are secure, use htmlspecialchars() on them
$genre = htmlspecialchars($_POST['genre']);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_track")) {
$acat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];
$tname = $_POST['name'];
$tfile = $_POST['file'];
$img = $_POST['img'];
$genre = $_POST['genre'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];


$first = mysql_query("INSERT INTO music (tname, tfile, img, genre, txt, cat) VALUES ('$name', '$file', '$img', $genre, '$txt', '$cat')") or die(mysql_error()); 
$first = mysql_query("INSERT INTO actions (acat, action, datee) VALUES ('$acat', '$action', $datee)") or die(mysql_error());

if(($first)&&($second))
{
$insertGoTo = "track_upload.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

}

Edited by stingerblue, 01 September 2007 - 10:56 AM.


#12 _*Creative Insanity_*

  • Guests

Posted 01 September 2007 - 12:05 PM

Yes it is just a redirect. Ok.. me thinks I have been working on this too long and making such stupid mistakes. Thanks for the kick stinger, I needed that. hehe

#13 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 01 September 2007 - 12:40 PM

You still aren't closing the parenthesis of either VALUES statements.

$first = mysql_query("INSERT INTO `music` (`name`,`file`,`img`,`genre`,`txt`,`cat`) VALUES ('$tname','$tfile','$img','$genre','$txt','$cat')")or die(mysql_error());

$second = mysql_query("INSERT INTO `actions` (`act_cat`,`action`,`datee`) VALUES ('$acat','$action','$datee')")or die(mysql_error());


#14 _*Creative Insanity_*

  • Guests

Posted 01 September 2007 - 01:46 PM

STILL just getting a blank page. Damn, now gotta look closer at this. Too long for something so simple.

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_track")) {
$acat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];
$tname = $_POST['name'];
$tfile = $_POST['file'];
$img = $_POST['img'];
$genre = $_POST['genre'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];


$first = mysql_query("INSERT INTO `music` (`name`,`file`,`img`,`genre`,`txt`,`cat`) VALUES ('$tname','$tfile','$img','$genre','$txt','$cat')")or die(mysql_error());

$second = mysql_query("INSERT INTO `actions` (`act_cat`,`action`,`datee`) VALUES ('$acat','$action','$datee')")or die(mysql_error());

if(($first)&&($second))
{
$insertGoTo = "track_upload.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

}

Ah HUH! got it! Thaniks for the help guys.. we got it!

This was the winner!

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_track")) {
$acat = $_POST['act_cat'];
$action = $_POST['action'];
$datee = $_POST['datee'];
$tname = $_POST['name'];
$tfile = $_POST['file'];
$img = $_POST['img'];
$genre = $_POST['genre'];
$txt = $_POST['txt'];
$cat = $_POST['cat'];


mysql_query("INSERT INTO `music` (`name`,`file`,`img`,`genre`,`txt`,`cat`) VALUES ('$tname','$tfile','$img','$genre','$txt','$cat')")or die(mysql_error());
mysql_query("INSERT INTO `actions` (`act_cat`,`action`,`datee`) VALUES ('$acat','$action','$datee')")or die(mysql_error());

$insertGoTo = "track_upload.php";
  if (isset($_SERVER['QUERY_STRING'])) {
	$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
	$insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

}

Edited by Creative Insanity, 01 September 2007 - 01:51 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users