Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Storing data from a form into a MySQL database! Help needed.

    Hello "codingforums" world.

    I'm having trouble inserting data from my html form into a MySQL database.
    This is the segment in my code that is not working.
    I successfully connect to the database but when I try to insert it I get an error message.

    My table name is onlineapp
    firstname is the field in the table
    $firstname is where I stored the cookie of the data from the first page of my form

    PHP Code:
    $sql "INSERT INTO onlineapp (firstname) VALUES ('$firstname')";

    if (!
    mysql_query($sql,$conn))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added"

    If you need more details please let me know because I've been stuck on trying to fix this without any success.
    Last edited by FishMonger; 04-07-2014 at 08:43 PM. Reason: Added code tags

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,912
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Please use the code tags whenever to post any code.

    What was the error message?

  • #3
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    This line might be one of the problems:
    PHP Code:
    $sql "INSERT INTO onlineapp (firstname) VALUES ('$firstname')"
    The variables value is not being properly concatenated to the string. Perhaps you're trying to do this?

    PHP Code:
    $sql "INSERT INTO onlineapp (firstname) VALUES ('" $firstname "')"
    Last edited by Linux_Sage; 04-07-2014 at 08:56 PM.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,912
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Unless the OP is using an old version of php, the $firstname variable will be interpolated in the double quoted string without the need to do the more verbose concatenation.

    If $firstname is an array and you need to print one of its members, then it would need to be wrapped with { } braces. The braces are optional with a simple var.
    PHP: Strings - Manual

    We'll need to wait for the OP to post the error message before we can figure out what is wrong.

  • #5
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ok this is my whole insert.php file.

    PHP Code:
    <html>
    <?php
    $dsn 
    "mysql:dbname=jpopovic";
    $username "jpopovic";
    $password "********";

    try {
      
    $conn = new PDO$dsn$username$password );
      
    $conn->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION );
    } catch ( 
    PDOException $e ) {
      echo 
    "Connection failed: " $e->getMessage();
    }

    $firstname $_COOKIE[firstname];
    $lastname $_COOKIE[lastname];
    $sex $_COOKIE[sex];
    $month $_COOKIE[month];
    $street $_COOKIE[street];
    $city $_COOKIE[city];
    $state $_COOKIE[state];
    $zip $_COOKIE[zip];
    $phone $_COOKIE[phone];
    $major $_COOKIE[major];
    $ssn $_COOKIE[ssn];
    $highschool $_COOKIE[highschool];
    $graddate $_COOKIE[graddate];
    $gpa $_COOKIE[gpa];
    $rank $_COOKIE[rank];
    $college1 $_COOKIE[college1];
    $college2 $_COOKIE[college2];
    $college3 $_COOKIE[college3];
    $collegeyear $_COOKIE[collegeyear];
    $credits $_COOKIE[credits];
    $classification $_COOKIE[classification];

    echo 
    $firstname;
    $sql "INSERT INTO onlineapp (firstname) VALUES ('" $firstname "')"

    if (!
    mysql_query($sql,$conn))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";

    mysql_close($conn)
    ?>
    <body>
    </body>
    </html>

    I'm trying to store all data in the form into a database that I already created.

    The result when this runs is

    "JankoError."

    Janko is because of the echo I put there just to try if the information is taken and saved.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The problem here is your mixing and matching connection libraries. You can't use PDO in conjunction with mysql libraries. There is no error associated simply because there is no dbms connection; if you open your error reporting up, there'd be an error indicating that there is no valid resource to execute the query against.
    PHP Code:
    if ($qry $conn->prepare("INSERT INTO onlineapp (firstname) VALUES (?)"))
    {
        if (!
    $qry->execute(array($firstname)))
        {
            
    $err $qry->errorInfo();
            die(
    $err[2]);
        }

    I don't typically do PDO, but that appears like it should work.

    Also, is there a reason for all the cookies? Cookies should be your last resort for storing information on a user; $_SESSIONS would be preferred over cookies since they cannot be modified by a client.

    Also, all of these associative offsets in the $_COOKIES array should be wrapped in quotations. It works only because of PHP's string fallback, but it will trigger errors (check the log, or use
    Code:
    ini_set('display_errors'); error_reporting(E_ALL);
    to see them).
    Last edited by Fou-Lu; 04-07-2014 at 09:40 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Janko (04-09-2014)

  • #7
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I used cookies because I was told to do them as a part of an instruction to complete the assignment. I'm basically new to the MySQL and totally confused now lol. These are all of my files so far.

    Code:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Online Application</title>
    </head>
    
    <body>
    <h1> Instruction for Application - Academic Year 2014-2015</h1>
    <ol>
    <li>The deadline for 2014-2015 scholarship application is March 30, 2014.</li>
    <li>Only completed application will be considered. DO NOT LEAVE ITEMS BLANK!</li>
    </ol><br />
    
    <form name="capstone" action="2ndstep.php" method="post">
    <fieldset>
    <legend>
    Personal Information
    </legend>
    
    
    <label class="title" for="firstname">First Name:</label>
    <input type="text" name="firstname" id="firstname"><br />
    <label class="title" for="lastname">Last name:</label>
    <input type="text" name="lastname" id="lastname"><br />
    <span class="title">Select your gender:</span> 
    <label><input type="radio" name="sex" value="male" />Male</label>
    <label><input type="radio" name="sex" value="female" />Female</label><br />
    
    <label class="title" for="birthday">Select your date of birth:</label>
    <select name="month" id="month">
    <option value="January">January</option>
    <option value="February">February</option>
    <option value="March">March</option>
    <option value="April">April</option>
    <option value="May">May</option>
    <option value="June">June</option>
    <option value="July">July</option>
    <option value="August">August</option>
    <option value="September">September</option>
    <option value="October">October</option>
    <option value="November">November</option>
    <option value="December">December</option>
    </select>
    <select name="day" id="day">
    <option value="1.">1</option>
    <option value="2.">2</option>
    <option value="3.">3</option>
    <option value="4.">4</option>
    <option value="5.">5</option>
    <option value="6.">6</option>
    <option value="7.">7</option>
    <option value="8.">8</option>
    <option value="9.">9</option>
    <option value="10.">10</option>
    <option value="11.">11</option>
    <option value="12.">12</option>
    <option value="13.">13</option>
    <option value="14.">14</option>
    <option value="15.">15</option>
    <option value="16.">16</option>
    <option value="17.">17</option>
    <option value="18.">18</option>
    <option value="19.">19</option>
    <option value="20.">20</option>
    <option value="21.">21</option>
    <option value="22.">22</option>
    <option value="23.">23</option>
    <option value="24.">24</option>
    <option value="25.">25</option>
    <option value="26.">26</option>
    <option value="27.">27</option>
    <option value="28.">28</option>
    <option value="29.">29</option>
    <option value="30.">30</option>
    <option value="31.">31</option>
    </select>
    <input type="text" name="year" id="year"><br />
    <label class="title" for="street">Street: </label>
    <input type="text" name="street" id="street"><br />
    <label class="title" for="city">City: </label>
    <input type="text" name="city" id="city"><br />
    <label class="title" for="state">State: </label>
    <input type="text" name="state" id="state"><br />
    <label class="title" for="zip">Zip: </label>
    <input type="text" name="zip" id="zip"><br />
    <label class="title" for="phone">Phone number: </label>
    <input type="text" name="phone" id="phone"><br />
    <label for="major" class="title">Intended major:</label>
    <select name="major" id="major">
    <option value="Information Technology">Information Technology</option>
    <option value="Computer Science">Computer Science</option>
    </select><br />
    <label class="title" for="ssn">Social Security Number: </label>
    <input type="text" name="ssn" id="ssn"><br />
    </fieldset>
    <div class="submit"><input type="submit" value="Next Page" /></div>
    </form>
    
    </body>
    </html>
    Second file:

    PHP Code:
    <?php
       setcookie
    ("firstname"$_POST["firstname"], time()+60*60*24*365);
       
    setcookie("lastname"$_POST["lastname"], time()+60*60*24*365);
       
    setcookie("sex"$_POST["sex"], time()+60*60*24*365);
       
    setcookie("month"$_POST["month"], time()+60*60*24*365);
       
    setcookie("day"$_POST["day"], time()+60*60*24*365);
       
    setcookie("year"$_POST["year"], time()+60*60*24*365);
       
    setcookie("lastname"$_POST["lastname"], time()+60*60*24*365);
       
    setcookie("street"$_POST["street"], time()+60*60*24*365);
       
    setcookie("city"$_POST["city"], time()+60*60*24*365);
       
    setcookie("state"$_POST["state"], time()+60*60*24*365);
       
    setcookie("zip"$_POST["zip"], time()+60*60*24*365);
       
    setcookie("phone"$_POST["phone"], time()+60*60*24*365);
       
    setcookie("major"$_POST["major"], time()+60*60*24*365);
       
    setcookie("ssn"$_POST["ssn"], time()+60*60*24*365);
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Online Application</title>
    </head>

    <body>
    <h1> High School Information</h1>

    <p> Enter your information about your high school</p> 


    <form name="capstone2.1" action="3rdstep.php" method="post">
    <fieldset>
    <legend>
    High School Information
    </legend>

    <label class="title" for="highschool"> High School attended: </label>
    <input type="text" name="highschool" id="highschool"><br />
    <label class="title" for="graddate"> Graduation Date: </label>
    <input type="text" name="graddate" id="graddate"><br />
    <label class="title" for="gpa"> Current GPA: </label>
    <input type="text" name="gpa" id="gpa"><br />
    <label class="title" for="rank"> Ranking in graduating class (%):</label>
    <input type="text" name="rank" id="rank"><br />
    </fieldset>
    <div class="submit">
      <input type="submit" value="Next Page" />
      <input type="button" value="Back" onClick="history.go(-1);return true;" />
     </div>

    </form>
    </body>
    </html>
    3rd page:

    PHP Code:
    <?php
       setcookie
    ("highschool"$_POST["highschool"], time()+60*60*24*365);
       
    setcookie("graddate"$_POST["graddate"], time()+60*60*24*365);
       
    setcookie("gpa"$_POST["gpa"], time()+60*60*24*365);
       
    setcookie("rank"$_POST["rank"], time()+60*60*24*365);
       
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Online Application</title>
    </head>

    <body>
    <h1> Previously Attended College </h1>

    <p> Enter previously attended colleges if any </p>

    <form name="capstone2.2" action="confirmation.php" method="post">
    <fieldset>
    <legend>
    College Information
    </legend>
    <label class="title" for="college1"> Previous college attended: </label>
    <input type="text" name="college1" id="college1"><br />
    <label class="title" for="college2"> Previous college attended: </label>
    <input type="text" name="college2" id="college2"><br />
    <label class="title" for="college3"> Previous college attended: </label>
    <input type="text" name="college3" id="college3"><br />
    <label class="title" for="collegeyear"> Year last attended: </label>
    <input type="text" name="collegeyear" id="collegeyear"><br />
    <label class="title" for"credits"> Number of credits earned: </label>
    <input type="text" name="credits" id="credits"><br />

    <label class="text" for="classification"> Classification entering The LeMoyne-Owen College: </label>
    <select name="classification" id="classification">
    <option value="Freshmen">Freshmen</option>
    <option value="Sophomore">Sophomore</option>
    <option value="Junior">Junior</option>
    <option value="Senior">Senior</option>
    </select>
    </fieldset>
    <div class="submit">
        <input type="submit" value="Complete" />
        <input type="button" value="Back" onClick="history.go(-1);return true;" />
    </div>

    </form>
    </body>
    </html>
    PHP Code:
    <?php
       setcookie
    ("college1"$_POST["college1"], time()+60*60*24*365);
       
    setcookie("college2"$_POST["college2"], time()+60*60*24*365);
       
    setcookie("college3"$_POST["college3"], time()+60*60*24*365);
       
    setcookie("collegeyear"$_POST["collegeyear"], time()+60*60*24*365);
       
    setcookie("credits"$_POST["credits"], time()+60*60*24*365);
       
    setcookie("classification"$_POST["classification"], time()+60*60*24*365);
       
    ?>

    <!doctype html>
    <html>
    <style>
    table, tr, td     {
        border: 1px solid #d6d6d6;
        background-color: #ffffff;
        line-height: 1.6em;
        width:30%;
        padding: 5px;
                    }
    .right     {
        text-align: left;
            }
    .left     {
        text-align: left;
        margin-left: 1em;
              }
    </style>
    <head>
    <meta charset="utf-8">
    <title>Entered Data</title>
    </head>
    <body>
    <form name="capstone2.3" action="insert.php" method="post">
    <p><table>
        <tr>
            <td class="left">First name</td>
            <td class="right"><?php echo $_COOKIE["firstname"?></td><br />
        </tr>
        <tr>
            <td class="left">Last name</td>
            <td class="right"><?php echo $_COOKIE["lastname"?></td><br />
        </tr>
        <tr>
            <td class="left">Gender</td>
            <td class="right"><?php if($_COOKIE["sex"] == "female")
                                echo 
    "Female";
                                else
                                echo 
    "Male";?></td>
        </tr>
        <tr>
            <td class="left">D.O.B.</td>
            <td class="right"><?php echo $_COOKIE["month"]; echo $_COOKIE["day"]; echo $_COOKIE["year"?></td><br />
        </tr>
        <tr>
            <td class="left">Street</td>
            <td class="right"><?php echo $_COOKIE["street"?></td><br />
        </tr>
        <tr>
            <td class="left">City</td>
            <td class="right"><?php echo $_COOKIE["city"?></td><br />
        </tr>
        <tr>
            <td class="left">State</td>
            <td class="right"><?php echo $_COOKIE["state"?></td><br />
        </tr>
        <tr>
            <td class="left">Zip</td>
            <td class="right"><?php echo $_COOKIE["zip"?></td><br />
        </tr>
        <tr>
            <td class="left">Phone</td>
            <td class="right"><?php echo $_COOKIE["phone"?></td><br />
        </tr>
        <tr>
            <td class="left">Major</td>
            <td class="right"><?php echo $_COOKIE["major"?></td><br />
        </tr>
        <tr>
            <td class="left">SSN</td>
            <td class="right"><?php echo $_COOKIE["ssn"?></td><br />
        </tr>
        <tr>
            <td class="left">High School</td>
            <td class="right"><?php echo $_COOKIE["highschool"?></td><br />
        </tr>
        <tr>
            <td class="left">Graduation Date</td>
            <td class="right"><?php echo $_COOKIE["graddate"?></td><br />
        </tr>
        <tr>
            <td class="left">GPA</td>
            <td class="right"><?php echo $_COOKIE["gpa"?></td><br />
        </tr>
        <tr>
            <td class="left">Rank</td>
            <td class="right"><?php echo $_COOKIE["rank"?></td><br />
        </tr>
        <tr>
              <td class="left">Previous College</td>
              <td class="right"><?php echo $_POST["college1"]; ?></td>    
          </tr>
        <tr>
              <td class="left">Previous College</td>
              <td class="right"><?php echo $_POST["college2"]; ?></td>        
          </tr>
        <tr>
              <td class="left">Previous College</td>
              <td class="right"><?php echo $_POST["college3"]; ?></td>        
          </tr>
        <tr>
              <td class="left">Year last attended</td>
              <td class="right"><?php echo $_POST["collegeyear"]; ?></td>        
          </tr>
        <tr>
              <td class="left">Number of credits earned</td>
              <td class="right"><?php echo $_POST["credits"]; ?></td>        
          </tr>
        <tr>
              <td class="left">Classification</td>
              <td class="right"><?php echo $_POST["classification"]; ?></td>        
          </tr>
    </table>
    </p>
    <div class="submit">
        <input type="submit" value="Complete" />
        <input type="button" value="Back" onClick="history.go(-1);return true;" />
    </div>
    </form>
    </body>
    </html>

    And the last one is that I posed in my last post

    PHP Code:
    <html>
    <?php
    $dsn 
    "mysql:dbname=jpopovic";
    $username "jpopovic";
    $password "iskusni0124";

    try {
      
    $conn = new PDO$dsn$username$password );
      
    $conn->setAttributePDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION );
    } catch ( 
    PDOException $e ) {
      echo 
    "Connection failed: " $e->getMessage();
    }

    $firstname $_COOKIE[firstname];
    $lastname $_COOKIE[lastname];
    $sex $_COOKIE[sex];
    $month $_COOKIE[month];
    $street $_COOKIE[street];
    $city $_COOKIE[city];
    $state $_COOKIE[state];
    $zip $_COOKIE[zip];
    $phone $_COOKIE[phone];
    $major $_COOKIE[major];
    $ssn $_COOKIE[ssn];
    $highschool $_COOKIE[highschool];
    $graddate $_COOKIE[graddate];
    $gpa $_COOKIE[gpa];
    $rank $_COOKIE[rank];
    $college1 $_COOKIE[college1];
    $college2 $_COOKIE[college2];
    $college3 $_COOKIE[college3];
    $collegeyear $_COOKIE[collegeyear];
    $credits $_COOKIE[credits];
    $classification $_COOKIE[classification];

    echo 
    $firstname;
    $sql "INSERT INTO onlineapp (firstname) VALUES ('" $firstname "')"

    if (!
    mysql_query($sql,$conn))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";

    mysql_close($conn)
    ?>
    <body>
    </body>
    </html>

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    OIC, its for an assignment that explains the cookies.
    Just gotta fix up the insertion then, chances are you should use the PDO or Mysqli libraries since the mysql library will soon be removed from the language. Stick with the PDO since you've already started with it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Janko (04-09-2014)

  • #9
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Any chance you could help me out on how to finish the insert.php file with PDO since I'm stuck at it?

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The code I posted in #6 should replace your existing query.
    Just replace this with the code posted above:
    PHP Code:
    echo $firstname;
    $sql "INSERT INTO onlineapp (firstname) VALUES ('" $firstname "')"

    if (!
    mysql_query($sql,$conn))
      {
      die(
    'Error: ' mysql_error());
      }
    echo 
    "1 record added";

    mysql_close($conn
    And assuming I didn't biff it, that should work as expected.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Janko (04-09-2014)

  • #11
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It worked, now I have to add all other cookies into the rest of the fields I have in the table. Would it look something like this? I'm guessing something is off since it didn't result into a record added to the table.

    PHP Code:
    if ($qry $conn->prepare("INSERT INTO onlineapp (firstname, lastname, sex, birthday, street, city, state, zip, phone, major, ssn, highschool, graddate, gpa, rank, college1, college2, college3, collegeyear, credits, classification) VALUES (?)"))
    {
        if (!
    $qry->execute(array($firstname$lastname$sex$month$street$city$state$zip$phone$major$ssn$highschool$graddate$gpa$rank$college1$college2$college3$collegeyear$credits$classification)))
        {
            
    $err $qry->errorInfo();
            die(
    $err[2]);
        }


  • #12
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I got it to work! Just had to add 21 question marks to my VALUES parenthesis. Lol Thank you sir I really appreciate the help!

  • #13
    New Coder
    Join Date
    Apr 2014
    Location
    Memphis, Tennessee
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    My project is successfully working but I've got an assignment that I have to do that is supposed to ask the user of it for the Last Name of a record in the database and supposed to get an answer what their phone number is. Is there any chance anybody could help me with this?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •