two insert queries in my code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tara83
    New Member
    • Feb 2007
    • 11

    two insert queries in my code

    Hi everyone

    i'm trying to put 2 sql queries in my code,

    i have two tables journal, journal1. one query will insert values in the journal table and the othe query will insert value in the journal1 table

    [PHP]<?php


    $self = $_SERVER['PHP_SELF'];
    $ISSN = $_POST['ISSN'];
    $ID = $_POST['ID'];
    $Title = $_POST['Title'];
    $Volume = $_POST['Volume'];
    $Number = $_POST['Number'];
    ?>

    <form action="<?php echo( $self ); ?>" method="post">
    ISSN: <input type="text" name="ISSN" size="3">
    ID: <input type="text" name="ID" size="14">
    Title: <input type="text" name="Title" size="14">
    Volume: <input type="text" name="Volume" size="8">
    Number: <input type="text" name="Number" size="8"><br>
    <input type="submit" value="Submit">
    </form>

    <?php

    if( $ISSN and $ID and $Title and $Volume and $Number)
    {
    $conn=@mysql_co nnect( "*****", "****", "*****" ) or die( "Err:Conn" );

    #select the specified database
    $rs = @mysql_select_d b( "*****", $conn) or die( "Err:Db" );

    #create the query
    $sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" )";
    ############### ###############
    $sql2 = "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\") "
    ############### ###############
    #execute query
    $rs = mysql_query( $sql, $conn );

    if( $rs )
    {
    echo( "Record added:$ISSN $Title $Volume $Number" );
    }
    else
    {
    echo("Record not added");
    }
    }

    ?>
    [/PHP]


    hope any one can help
  • xwero
    New Member
    • Feb 2007
    • 99

    #2
    add a semicolon after the statement in the first sql variable.

    a semicolon tells mysql this is the end of the statement like it does in php.

    [PHP]
    $sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" );";
    $sql .= "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\") "
    [/PHP]

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      You could combine them but then it is difficult to pinpoint the error to the exact query. The following will do it for 2 separate statements[php]$sql = "insert into journal ( ISSN, Title, Volume, Number ) values ( $ISSN, \"$Title\", \"$Volume\", \"$Number\" )";
      $sql2 = "insert into journal1 (ID, ISSN) values ($ID,\"$ISSN\") "
      // ----------------
      // execute query 1
      // ----------------
      if ($rs = mysql_query( $sql, $conn ))
      or die("Record from query 1 not added: error = ".mysql_error() );
      else {
      echo "Journal record added: $ISSN $Title $Volume $Number";
      // ----------------
      // execute query 2
      // ----------------
      if ($rs2 = mysql_query( $sql2, $conn ))
      or die("Record from query 2 not added: error = ".mysql_error() );
      else
      echo "Journal1 record added:$ISSN";
      }

      ?>[/php]
      Ronald :cool:

      Comment

      • Tara83
        New Member
        • Feb 2007
        • 11

        #4
        thnx for both of you, but it seems that i wont be able to make it like this

        after searching on the net i found out that php wont allow more than one query in one code.

        yet i can create a function on my own to handle multiple query.

        if any one knows a simple function to allow multiple qurey, i'll be greatful to see it.

        Comment

        • xwero
          New Member
          • Feb 2007
          • 99

          #5
          if you want to create your own function i think it's best to split it up according to the sql statements; select, insert, replace, update,delete.

          the delete could be something like

          [PHP]
          function multipleDelete( $queries){
          $affectedRows = array();
          foreach($querie s as $query){
          $result = mysql_query($qu ery);
          $affectedRows[] = mysql_affected_ rows()
          }
          return $affectedRows;
          }
          [/PHP]
          The insert,replace and update can be similar but for the select you need to return your result instead of the row number.

          This is the function in the simpelest form you best add error checks and other security measures

          Comment

          Working...