Jump to content

[SOLVED] Inserting a record in to a mySQL Data Base


Recommended Posts

Dear all,

 

I've never done anything in PHP before, so please bear with me. 

 

I've been asked to write something that will allow a user to manage a dictionary, and I've written a bit of code that'll take input from a form, and store it in a mySQL data base.

 

The data base structure is as follows:

 

ID - AutoIncremement

Term - Varchar (100)

Definition - Varchar (150)

Chapter - Integer

Section - Integer

 

I've written the following code, which loads fine.  And will allow the user to seemingly go through the data entry bit.  But when I look in MySQL table, there's no records there.

 

I'm using PHP version 5.2.6 which came with my XAMP installation.  And I think I'm using mySQL 5.067

 

Any help would be awesome. 

 

Thanks

 

Pluto

 

<?php

if($_POST[op] != "add")
{
    $display_block = "<h1>Add an Entry</h1>
    <form method=\"post\" action=\"$_SERVER[php_SELF]\">
    <p><strong>Term:</strong><br>
    <input type=\"text\" name=\"term\" size=50 maxlength=75>
    
    <p><strong>Definition:</strong><br>
    <input type=\"text\" name=\"definition\" size=50 maxlength=75>
    
    <p><strong>Chapter:</strong><br>
    <input type=\"text\" name=\"chapter\" size=30 maxlength=75>
    
    <p><strong>Section:</strong><br>
    <input type=\"text\" name=\"section\" size=30 maxlength=75>
    
    <p><input type=\"submit\" name=\"submit\" value=\"Add Word\"></p>
    </form>";
}
else if($_POST[op]=="add")
{
    // time to add to tables, so check for fields
    if(($P_POST[term]=="")||($P_POST[definition]==""))
    {
        header("Location: test.php");
        exit;
    }
    
    // connect to the data base.
    
    $conn = mysql_connect("localhost","root","") ;
    
    mysql_select_db("v1test",$conn);
    
    // add to records to table
    $add_word = "insert into englishtest (id, term,definition, chapter, section) values (' ','$_POST[term]','$_POST[definition]','$_POST[chapter]','$_POST[section]')";
    mysql_query($add_word) ;
    //$word_id = mysql_insert_id();
}
?>

<HTML>
<HEAD>
<TITLE>Add an Entry </TITLE>
<BODY>
<?php echo $display_block;?>
</BODY>
</HTML>

   

 

Out of curiosity, why would you write this in a language you've never touched before?

 

Well I did try and let the dude let me write this in ASP.NET, but he said no, PHP...

 

So I'm learning as I go...

 

and not doing a good job it would seem lol

Must confess that much of this I've learned from a book, and I've looked and looked, and it doesn't seem to define op anywhere.  Which is kind of annoying.  Which makes me wonder how this script ran in the first place.

 

I've tried a few bits, but the book isn't forthcoming. 

 

If anyone has any advice, I'd be greatful.

 

And thanks for the responses already.

 

Pluto

    $display_block = "<h1>Add an Entry</h1>
    <form method=\"post\" action=\"$_SERVER[php_SELF]\">
    <p><strong>Term:</strong><br>
    <input type=\"text\" name=\"term\" size=50 maxlength=75>
    
    <p><strong>Definition:</strong><br>
    <input type=\"text\" name=\"definition\" size=50 maxlength=75>
    
    <p><strong>Chapter:</strong><br>
    <input type=\"text\" name=\"chapter\" size=30 maxlength=75>
    
    <p><strong>Section:</strong><br>
    <input type=\"text\" name=\"section\" size=30 maxlength=75>
    <input type=\"hidden\" name=\"op\" value=\"yes\" />
    <p><input type=\"submit\" name=\"submit\" value=\"Add Word\"></p>
    </form>";

 

Add it via a hidden input box. I would also surround it in ' (single quotes) when you access it via post:

$_POST['op']

 

So sorry for my ineptitude at this, but I've made the changes that you suggested, and it still won't send anything to the data base.  Even though the user has rights to insert etc.

 

I'm now at a loss as to why this shouldn't work.

 

Also when I add single quotes (') to the $_POST[fieldname] lines, e.g term, etc.  It throws a hissy.  The error I get is as follows. 

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\test.php on line 38

 

also I'm on msn messenger.  emyrwilliams at hotmail dot com

 

Pluto

Try this:

 

if(isset($_POST['submit'])) {
echo "You just tried to submit hahaha";
}

if($_POST['op'] != "add")
{
    $display_block = "Add an Entry
    </pre>
<form method='\"post\"' action="%5C%22%24_SERVER%5Bphp_SELF%5D%5C%22">
    
Term:

    
    
    
Definition:

    
    
    
Chapter:

    
    
    
Section:

    
    
    

    </form>";<br>}<br>else if($_POST['op']=="add")<br>{<br>$display_block = "<h1>Got to the if statement!!!</h1>";<br>    // time to add to tables, so check for fields<br>    if(($P_POST['term']=="")||($P_POST['definition']==""))<br>    {<br>        header("Location: test.php");<br>        exit;<br>    }<br>    <br>    // connect to the data base.<br>    $conn = mysql_connect("localhost","root","") ;<br>    mysql_select_db("v1test",$conn);<br>    <br>    // add to records to table<br>    $add_word = "insert into englishtest (id, term,definition, chapter, section) values ('','{$_POST['term']}','{$_POST['definition']}','{$_POST['chapter']}','{$_POST['section']}')";<br>    mysql_query($add_word) or die(mysql_error());<br>    //$word_id = mysql_insert_id();<br>}<br>?><br><br><br><br><title>Add an Entry </title>
<br><br><b

Try this:

 

<?php

if(isset($_POST['submit'])) {
echo "You just tried to submit hahaha";
}

if($_POST['op'] != "add")
{
    $display_block = "<h1>Add an Entry</h1>
    <form method=\"post\" action=\"$_SERVER[php_SELF]\">
    <p><strong>Term:</strong><br>
    <input type=\"text\" name=\"term\" size=50 maxlength=75>
    
    <p><strong>Definition:</strong><br>
    <input type=\"text\" name=\"definition\" size=50 maxlength=75>
    
    <p><strong>Chapter:</strong><br>
    <input type=\"text\" name=\"chapter\" size=30 maxlength=75>
    
    <p><strong>Section:</strong><br>
    <input type=\"text\" name=\"section\" size=30 maxlength=75>
    <input type=\"hidden\" name=\"op\" value=\"yes\" />
    <p><input type=\"submit\" name=\"submit\" value=\"Add Word\"></p>
    </form>";
}
else if($_POST['op']=="add")
{
$display_block = "<h1>Got to the if statement!!!</h1>";
    // time to add to tables, so check for fields
    if(($P_POST['term']=="")||($P_POST['definition']==""))
    {
        header("Location: test.php");
        exit;
    }
    
    // connect to the data base.
    $conn = mysql_connect("localhost","root","") ;
    mysql_select_db("v1test",$conn);
    
    // add to records to table
    $add_word = "insert into englishtest (id, term,definition, chapter, section) values ('','{$_POST['term']}','{$_POST['definition']}','{$_POST['chapter']}','{$_POST['section']}')";
    mysql_query($add_word) or die(mysql_error());
    //$word_id = mysql_insert_id();
}
?>

<HTML>
<HEAD>
<TITLE>Add an Entry </TITLE>
<BODY>
<?php echo $display_block;?>
</BODY>
</HTML>

 

Weird...that didn't do it either....got the data base name right, the table name right and everything....

 

I got the echo message at the top, however nothign about got to the if statement, so it's not getting to the bit that's actually inserting the records for some reason.

 

Also in the course of looking through it, I found that op value was Yes rather than add, so I changed that over, then got this error

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test2.php:4) in C:\xampp\htdocs\test2.php on line 32

 

Thanks

 

Pluto :)

 

 

 

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test2.php:4) in C:\xampp\htdocs\test2.php on line 32

 

Yes, you get this error message because you're echoing before your redirect (my fault).  Take out these lines:

 

if(isset($_POST['submit'])) {
   echo "You just tried to submit hahaha";
}

 

Where does the the $P_POST array come from?

 

$P_POST['term'];
$P_POST['definition'];

My understanding is that they're superglobals, so I don't need to define them anywhere.

 

And my (limited) understanding was that you use the $_POST on the variables you want to add to the POST request to the server.

 

Or am I wrong?

 

Sorry, this is the first thing I've ever written in PHP

Dear all,

 

I have since resolved the issue. 

 

I found a good book on the subject, and figured I'd follow their example.  They seperated the HTML from the PHP, so that way, it would be clearer to edit and maintain.

 

So I now have two seperate files.  Their code is as follows.

 

insert_test.html - This will be re-written to include java script at some point.  As I'm going to be doing things in different languages.

 

<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Insert a record one word at a time</title>
    </head>
    <body>
     <FORM ACTION="insert_word.php" METHOD=POST>
         <p> Text to add:<br/>
         <input type=text name="word" size=30><br>             
         <input type=text name="definition" size=30><br>
         <input type=text name="chapter" size=30><br>
         <input type=text name="section" size=30><br>
            
         <p><input type=submit name="submit" value="Insert Record"></input>             
         </FORM>
    </body>
</html>

 

insert_word.php

 

<?php
    // Open the data base connection
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("test",$conn);
    
    $sql = "INSERT INTO test2 values ('','$_POST[word]','$_POST[definition]','$_POST[chapter]','$_POST[section]')";
    if(mysql_query($sql, $conn))
    {
        echo "record added";
    }
    else
    {
        echo "Error in entering the record";
    }
?>

 

So I would like to thank everyone for their help.  You've certainly got me on the right track, and I figured I should try and keep front and back end stuff seperate where possible.

 

This is the first step of the system I'm writing, so I'm sure you're going to see some more posts from me.  I'm looking forward to being part of the community.

 

Thanks once again

 

Pluto

Thanks Maq,

 

Now to check if the word is already in the data base...

 

fun...:)

 

Pluto

 

You should create a new thread for this particular problem.

 

I was going to have a stab at it myself first before posting.  I've gotta learn to do it, or at least have a go, or I'll learn nothing, and it'll be like others writing the code for me, and that's not good.

 

But if it comes to it, there will be another thread.

Thanks Maq,

 

Now to check if the word is already in the data base...

 

fun...:)

 

Pluto

 

You should create a new thread for this particular problem.

 

I was going to have a stab at it myself first before posting.  I've gotta learn to do it, or at least have a go, or I'll learn nothing, and it'll be like others writing the code for me, and that's not good.

 

But if it comes to it, there will be another thread.

 

Yes, I definitely agree with you.  That's a good mindset to have when you are learning to program.  What I really meant was, if you have a problem/issue/concern whatever, start a new thread for it.  :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.