0

I have a problem posting data in my database with a form. Very basic stuff I'm sure but Im quite stuck :/

I am able to get stuff out of my database using the select from database routine. So I know that the connection with the databse is probably not the problem.

This is my 'upload.php':

<html>
<head>
<title>Upload</title>
</head>
<body>

    <form action="action.php" method="post">

        <fieldset class="first">
             Name: 
        <input type="text" name="author" />
        Heading:
          <input type="text" name="heading" />

        Text:
        <textarea type="text" name="thecontent"> </textarea>
        </fieldset>


        <fieldset>
        <input type="submit"/> 
            </fieldset>
    </form>



</body>
</html>

And this is my 'action.php':

<html>
<head>

<title>Send!</title>
</head>

<body>

<?php

 ini_set('display_errors', 1); error_reporting(E_ALL); 

$link = mysql_connect('localhost','name','pasword')
or die ("Unable to connect"); 

$mydb = mysql_select_db('the_database',$link)
or die ("No database found"); 

$author = $_POST['author']; 
$heading = $_POST['heading'];
$thecontent = $_POST['thecontent'];

$mysql_query="INSERT INTO articles ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')" or die(mysql_error());   

echo "This was send: $author $heading $thecontent <br> ";


 mysql_close()

?> 

</body>
</html>

All help would be much appreciated!!

Cheers, Ziggy

Thanks for all the help guys! I'm trying to use mysqli to insert the data however it's not yet working this is my new code in action.php:

<html>
<head>

<title>Send!</title>
</head>

<body>

<?php

 ini_set('display_errors', 1); error_reporting(E_ALL); 
$DB_HOST = 'localhost';
$DB_USER = '**';
$DB_PASS = '***';
$DB_NAME = '***';
@ $db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
echo 'Error.';
exit();
}

$author = $_POST['author']; 
$heading = $_POST['heading'];
$thecontent = $_POST['thecontent'];

$query = 'INSERT INTO articles ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')';   

$result = $db->query($query);
    if ($result) {
    echo $db->affected_rows."This was added.";
    } 
    else {
    echo "somethings gone very wrong.";
    }

$db->close();




?> 

</body>
</html>

What am I doing wrong guys? Help is much appreciated!

Cheers, Ziggy

Ziggy
  • 35
  • 4

2 Answers2

1

You build the INSERT string, but you never call a method to realy INSERT the DB with it.

Moreover, old mysql_* methods are deprecated, use PDO or mysqli API instead, see http://www.php.net/manual/en/mysqlinfo.api.choosing.php

See also stackoverflow post about this : mysqli or PDO - what are the pros and cons?

Some PDO prepared statements examples with PDO : http://www.php.net/manual/en/pdo.prepare.php

Community
  • 1
  • 1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
-1
$mysql_query="INSERT INTO articles    ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')";
mysql_query($mysql_query);
//required to run the query..

And mysql_close(); // missing :p
Himanshu Jaju
  • 1,439
  • 1
  • 11
  • 16