Jump to content

Insert only posting one field :S


Recommended Posts

I have a form posting 3 things, title, user, and content and it only ends up inserting the content and date parts into the table.insert.php:

<?php$con=mysql_connect('localhost','root','*******');// Check connectionif (!$con)  {  echo "Failed to connect to MySQL: " . mysql_error();  }mysql_select_db("tutorials_blog2", $con);$sql="INSERT INTO posts (username, title, content, date)VALUES('$_POST[Username]','$_POST[Title]','$_POST[Content]', UNIX_TIMESTAMP())";if (!mysql_query($sql, $con))  {  die('Error: ' . mysql_error());  }echo "1 record added";mysql_close($con);?>

This is the only place i can see anything being wrong but i'm not sure what's the problem. maybe my form?

<form action="insert.php" method="post">Username: <input type="text" name"Username">Title: <input type="text" name"Title" size="30">Content: <input type="text" name="Content" class="inputtext"><input type="submit">

Please help. ty

Link to comment
Share on other sites

<form action="insert.php" method="post">Username: <input type="text" name"Username" />Title: <input type="text" name"Title" size="30" />Content: <input type="text" name="Content" class="inputtext" /><input type="submit" /></form> <!--Add this u forget close tag -->

Edited by fikiwan
Link to comment
Share on other sites

You're missing equal signs in your form: name"Username" But for the love of God, stop using the mysql_* functions. They're old, deprecated, slow, ugly, and oh yeah... insecure as ######. With your code right now, I promise you you'll get hacked. Use PDO instead. You won't regret it.

<?php$dsn = 'mysql:dbname=tutorials_blog2;host=';$user = 'root';$password = '';$db = new PDO($dsn, $user, $password);$stmt = $db->prepare("    INSERT INTO posts (username, title, content, date)    VALUES (?, ?, ?, UNIX_TIMESTAMP())");$result = $stmt->execute([$_POST['Username'], $_POST['Title'], $_POST['Content']]);if ($result){    echo '1 record added';}?>

It looks complicated at first sight, but trust me, it's worth it.Read more here:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developersphp.net/pdo

Edited by Nico
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...