Jump to content

[SOLVED] newb insert trouble


Time Traveller

Recommended Posts

Hi folks, Im a newb to both php and mysql. Im working through a tutorial and having a problem of inserting data into a table.

I have installed xampp and am using the standard windows install with no modifications. In mysql I have created a new database with phpmyadmin and setup a table with the following fields:
    id int(6) auto_increment
    name VARCHAR(20)
    addr VARCHAR(100)
    desc VARCHAR(200)

Here is the code

index.php
[code]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
<link rel="stylesheet" href="index.css" type="text/css">
</head>

<body>
    <div id="wrap">

        <div id="pageHead">
            <?php
                echo date("D M j G:i:s T Y");
            ?>
        </div>
       
        <div id="pageInfo">
            Website Links
        </div>

        <div id="linkAddr"><b>Name</b></div>
        <div id="linkDesc"><b>Description</b>
        </div>
        <div id="linkDel"></div>
        <br id="fClear" />
        <br />

        <div id="AddNewForm">

            <form action=form.php method=post>
                <table>
                    <tr>
                        <td>Name</td><td><input type="text" name="name" size="20" /></td>
                    </tr>
                    <tr>
                        <td>Address</td><td><input type="text" name="addr" size="80" /></td>
                    </tr>
                    <tr>
                        <td>Description</td><td><input type="text" name="desc" size="80" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Add Link" name="addNew" /></td>
                    </tr>
                </table>
            </form>

        </div>
    </div>
</body>

</html>
[/code]

And form.php
[code]
<?
    echo "Starting form.php script.<br />";
    $link = mysql_connect('localhost', 'root', 'rootpass') or die("Could not connect: " . mysql_error());
    echo "Connected to mysql.<br />";
    @mysql_select_db('linkdb') or die("Could not select database");
    echo "Database selected.<br />";
    if($_POST['name']!=""){
        $linkN=htmlentities($_POST['name']);
    $linkA=htmlentities($_POST['addr']);
    $linkD=htmlentities($_POST['desc']);
    mysql_query("INSERT INTO links (name, addr, desc) VALUES('$linkN','$linkA','$linkD');");
    echo "Link: $linkN inserted.<br />";
    }
    mysql_close($link);
    echo "Connection closed.<br />";
?>
[/code]

When I submit a new record, form.php echos every message yet when I browse the rows in phpmyadmin there is nothing other than those which I entered through phpmyadmin. Can anyone point out what I'm doing wrong? Thanks.
Link to comment
https://forums.phpfreaks.com/topic/35526-solved-newb-insert-trouble/
Share on other sites

Thanks,

I added this line to form.php at line #13 [code]echo mysql_errno($link) . ": " . mysql_error($link). "<br />";[/code]

When I submit the form, I get the following error:

[quote]1064: 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 'desc) VALUES('spam','eggs','ham')' at line 1[/quote]

I had tried putting all field names in quotes, have looked at mysql_query, and have compared my code to code that works on this setup. I dont see what the syntax error could be.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.