Page 1 of 1

Trying insert

Posted: Thu Oct 16, 2008 9:50 pm
by grose
Just started learning PHP and am having trouble inserting data into mysql.

I have a form within index.php which sends the data to add2DB.php for processing

Code: Select all

 
<form  action="add2DB.php" method="POST">          
    <input type="text" name="txtFrom" value="" />
    <input type="text" name="txtTo" value="" />                
</form>
 

add2DB.php

Code: Select all

 
<?php
    require('index.php');
 
    $dbHost = "localhost:3306";
    $database = "myTest";
    $username = "root";
    $password = "enter";
 
    $connection = mysql_connect($dbHost, $username, $password);
 
    if (!$connection)
    {
        die("Could not connect: <br>". mysql_error() );
    }
 
    //Get the data from index.php
    $to = $_POST[txtTo];
    $from = $_POST[txtFrom];
 
    mysql_select_db($database);
    mysql_query("INSERT INTO Map(`from`, `to`) VALUES ($from, $to)");
 
    echo "$from and $to have been added. <br />";
 
    static $count = 1;
    if ($count = 1)
    {
        echo "$count record added.";
    }
    else
    {
        echo "$count records added.";
    }
 
    mysql_close($connection);
?>
 
When I run this:

echo "$from and $to have been added. <br />"; produces the expected values so I assume $to = $_POST[txtTo] and $from = $_POST[txtFrom] is working as expected. That is the values for txtTo and txtFrom are being passed from index.php to add2DB.php.

I also tried the SQL query outside of PHP and it seems to work but nothing is being entered into the database when I run this program.

Also my count of records doesn't work but that's an aside for now.

I'm sure that I am missing something valuable here. Any help would be greatly appreciated.
Thanks

Re: Trying insert

Posted: Thu Oct 16, 2008 11:22 pm
by requinix
Try changing line 22 to say

Code: Select all

mysql_query("INSERT INTO Map(`from`, `to`) VALUES ($from, $to)") or die(mysql_error());
Then maybe you'll see the problem.

Re: Trying insert

Posted: Thu Oct 16, 2008 11:27 pm
by SBro
First of all, edit your post and hide out your access details to your DB :) Next, try this code instead:

Code: Select all

 
<?php
    require('index.php');
 
    $dbHost = "localhost:3306";
    $database = "****";
    $username = "****";
    $password = "****";
 
    $connection = mysql_connect($dbHost, $username, $password);
 
    if (!$connection)
    {
        die("Could not connect: <br>". mysql_error() );
    }
 
    //Get the data from index.php
    $to = $_POST['txtTo'];
    $from = $_POST['txtFrom'];
 
    mysql_select_db($database);
    mysql_query("INSERT INTO Map(`from`, `to`) VALUES ("$from", "$to")");
 
    echo "$from and $to have been added. <br />";
 
    static $count = 1;
    if ($count = 1)
    {
        echo "$count record added.";
    }
    else
    {
        echo "$count records added.";
    }
 
    mysql_close($connection);
?>
 
What I have changed:
  • You need to enclose the $_POST field names in quotes
  • When inserting character fields in mysql you will need to enclose them in quotes
I'm not sure what your trying to do with your $count variable, but it is doing nothing at the moment and it will always be '1'. Lastly you should think about escaping user input ($_POST variables) with something like mysql_real_escape_string()

Re: Trying insert

Posted: Thu Oct 16, 2008 11:54 pm
by grose
Thanks for the replies.

As I am learning and this is sitting on my home computer I didn't worry about the access details. If anyone can or would want to go to the trouble of hacking into my test website I will be stunned and amazed.

I did all that was suggested and the problem was the lack of quotes around $from and $to

mysql_query("INSERT INTO Map(`from`, `to`) VALUES ("$from", "$to")");

As for the, mysql_real_escape_string() I don't know what you are talking about but I will look it up.

The $count variable was supposed to track how many records had be added.

Thanks again.