Adding stuff to a mysql databases from an html form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r00m23
    New Member
    • Mar 2007
    • 3

    Adding stuff to a mysql databases from an html form?

    I have only been doing php for about 24 hours and have read about 50 tutorials discussing the subject. I am confused though because I can't get my script to work like I want it to. Last night I had a hard time figuring out how to connect to a data bases, but I got that part down now. Here is the code that I am struggling with.
    Code:
    <?php
    
    
    //get post data
    
    $mmame = addslashes($_Post['Name']);
    
    //execute the SQL query and return records
    $frrt = 'INSERT INTO `Projects` (`Name`, `munb`, `address1`, `address2`, `city`, `stae`, `zip`, `email`, `dec`) VALUES ('$mmame', NULL, \'tree\', \'speed\', 
    
    \'weel\', \'vop\', \'nosit\', \'mooe\', \'beooe\');';
    
    $result = mysql_query($frrt) or die("$query FAILED because ".mysql_error());
      echo "Thank you! Information entered.<a href='swift4.php'>swift4</a>\n '";
    ?>

    But if

    Code:
    $frrt = 'INSERT INTO `Projects` (`Name`, `munb`, `address1`, `address2`, `city`, `stae`, `zip`, `email`, `dec`) VALUES (\'gfgnxx\', NULL, 
    
    \'fdsgnhdsfgbsdfg\', \'fgbf\', \'gfg\', \'fgnfg\', \'sfgbdfsgnb\', \'dfgndfgn\', \'dfghnghmngh\');';
    the code will work, but obviously it does not take any thing from the form.

    I am assuming I don't understand how variables work in php. Can ne one see what I am doing wrong?
    Last edited by r00m23; Mar 28 '07, 04:08 PM. Reason: typo,,
  • federicog
    New Member
    • Mar 2007
    • 30

    #2
    I am assuming I don't understand how variables work in php. Can ne one see what I am doing wrong?
    Exactly, that's the problem ;)
    In order for PHP to process a string as a variable, you must either use concatenation or double quotes. When you use single quotes, everything is parsed as a string:

    [PHP]$var = 1;

    echo $var; //will output 1
    echo "var: $var"; //will output var: 1
    echo "var: ".$var //will output var: 1
    echo 'var: $var'; //will output var: $var
    [/PHP]

    So, you should use concatenation in your SQL for it to work properly:

    [PHP]$query = "INSERT INTO table VALUES('".$stri ng."', '".$string2. "', 'random text');"[/PHP]

    You should also use mysql_real_esca pe_string() before sending data to mysql.

    Good Luck!

    Comment

    • r00m23
      New Member
      • Mar 2007
      • 3

      #3
      Originally posted by federicog
      So, you should use concatenation in your SQL for it to work properly:

      [PHP]$query = "INSERT INTO table VALUES('".$stri ng."', '".$string2. "', 'random text');"[/PHP]

      You should also use mysql_real_esca pe_string() before sending data to mysql.
      Thanks for that info. It still does not work however. All I get is a blank page,, with no error msg.

      In my code what does the \' mean? Could that have ne thing to do with this thing not working?

      [PHP]<?php


      //get post data

      $mmame = mysql_real_esca pe_string($_Pos t['Name']);

      //execute the SQL query and return records
      $frrt = 'INSERT INTO `Projects` (`Name`, `munb`, `address1`, `address2`, `city`, `stae`, `zip`, `email`, `dec`) VALUES ('".$mmame."' , NULL, \'tree\',

      \'speed\', \'weel\', \'vop\', \'nosit\', \'mooe\', \'beooe\');';

      $result = mysql_query($fr rt) or die("$query FAILED because ".mysql_error() );
      echo "Thank you! Information entered. <a href='swift4.ph p'>swift4</a>\n '";
      ?>[/PHP]

      btw, the form is submited on a page named swift2.html using the methoid post.
      Im working on swift3.php and I can see the results on swift4.php

      VVswift4.php connects and does this. It works like it shouldVV[PHP]
      <?php
      //execute the SQL query and return records
      $frrt = 'SELECT * FROM `projects` LIMIT 0, 30 ';
      $result = mysql_query($fr rt);

      //fetch tha data from the database
      while ($row = mysql_fetch_arr ay($result)) {
      echo "ID:".$row{'mun b'}." Name:".$row{'Na me'}." City:".$row{'ci ty'}." Description:".$ row{'dec'}."<br />";
      }
      ?>
      [/PHP]
      So as far as I know swift3.php should work,, but I am missing something.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        The \ character inside a string is used when you want to add characters that would usually mess up how your code is parsed.

        For example, if you wanted to add a double quote mark into a string. You cant do that by just adding the " char because that would close the string and mess up your code. So we put \ in front of the " char so the parser knows to handle that as a " char instead of closing the string.

        PHP handles strings unlike most other programming languages. In PHP a text inside a double quote mark, "like this", is parsed normally. You can put variable names inside the string and it will print out the value of the variable.
        If you, however, create a string using single quote marks, 'like this', the PHP parser will assume that the text is supposed to be written exactly like it is written, so if you use variable names inside that, it will print out the variable name, rather than its value.

        A couple of examples
        [PHP]
        $var = "Hi";

        echo "$var"; // output: Hi
        echo '$var'; // output: $var

        echo "$var - {$var}"; // output: Hi - Hi
        echo '$var - {$var}'; //output: $var - {$var}

        echo " Double \" Single ' ";// output: Double " Single '
        echo ' Double " Single \' '; // output: Double " Single '
        echo " Signs - \" \' \\ "; // output: Signs - " ' \
        [/PHP]

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          I created a little example script that might help you with your project.

          This is the MySQL query to create the database I use
          Code:
          CREATE TABLE FromPost
          (
            iID SERIAL Primary Key,
            iName VARCHAR(255) NOT NULL,
            iValue VARCHAR(255) NOT NULL
          )
          This is the code
          [PHP]
          <form name="at" id="at" action="#" method="post">
          <input type="text" name="Name" value="Name" />
          <br /><input type="text" name="Value" value="Value" />
          <br />
          <br /><input type="submit" value="Add" />
          </form>

          <p>
          <?php
          // Connect database
          $DB = mysql_connect(" localhost", "Atli", "Password") ;
          mysql_select_db ("tests", $DB);

          // Add if anything was posted
          if(isset($_POST['Name'])) {
          $QUERY = "INSERT INTO FromPost(iName, iValue) VALUES('". $_POST['Name'] ."', '". $_POST['Value'] ."')";
          $RESULT = mysql_query($QU ERY);

          if(!!$RESULT) {
          echo "Values added";
          }
          else {
          echo "Values failed to be added";
          }
          }

          // Print all existing rows
          $QUERY = "SELECT iName, iValue FROM FromPost";
          $RESULT = mysql_query($QU ERY);

          if(mysql_num_ro ws($RESULT) > 0) {
          echo "<p>Rows in table: <ol>";
          while($row = mysql_fetch_ass oc($RESULT)) {
          echo "<li>". $row['iName'] ." = ". $row['iValue'] ."</li>";
          }
          echo "</ol></p>";
          }
          else {
          echo "<p>No rows exist in the table</p>";
          }
          ?>
          </p>
          [/PHP]

          Hope this helps

          Comment

          • r00m23
            New Member
            • Mar 2007
            • 3

            #6
            Thanks guys(or gals,).. I got my code working.(I think.) I have a new problem that I think has to do with the post data info but I am going to read around a bit first and since it does not have ne thing to do with php ill post that somewhere else if I can't figure it out this week. Again thanks for all the help with understanding variables. You all showed the same info as the tutorials only applying it to my situation, witch help clear up my confusions I had.

            The new code looks like this.[PHP]<?php



            //execute the SQL query and return records
            $frrt = "INSERT INTO `Projects` (`Name`, `munb`, `address1`, `address2`, `city`, `stae`, `zip`, `email`, `dec`,`stat`) VALUES ('".$_Post['Name']."', NULL,

            '".$_Post['Addres1']."','".$_Pos t['Addres2']."','".$_Pos t['City']."','".$_Pos t['state']."','".$_Pos t['zip']."','".$_Pos t['Email']."','".$_Pos t['dec']."','SUB

            MITED')";

            $result = mysql_query($fr rt) or die("$query FAILED because ".mysql_error() );
            echo "Thank you ! Information entered. <a href='swift4.ph p'>swift4</a>\n '";
            ?>[/PHP]

            Comment

            Working...