Forum Moderators: coopster

Message Too Old, No Replies

Add record script (php to MYSQL)

made a script that should create a new record in db

         

Twisted Mind

7:24 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Hello I made this script while creating an extreme simple CMS this script should make a new record in the database there is another collumn with id

This is the script:

<html>
<? include "dbconn.php";

$title = $_POST['title'];
echo "$title";
$link = $_POST['link'];
echo "$link";
$content = $_POST['content'];
echo "$content";

$query =
"
UPDATE example
SET title = '$title',
link = '$link',
content = '$content'";
$result = mysql_query($query,Guildwars) or die ("Error in query: $query " . mysql_error());

?>
</html>

Please help me :p

Anyango

7:29 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey!

For a new entry in a database, "UPDATE" statement of mysql isnt used, instead "INSERT INTO" is used, try modifying your query like

$query = " INSERT INTO example(title,link,content) VALUES ('$title','$link','$content')";

Regards ;)

Twisted Mind

7:30 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Thanks i really was struglling with this a couple of minutes :P

Twisted Mind

7:31 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



Another problem The result is wrong i should use someting else but i dont know what :)

Anyango

7:33 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although this message might not be needed at all but i would just like to explain a concern, the query you posted above, that has an UPDATE command without any "WHERE" clause specified, that means if you run that query, it will update ALL the existing records of table "example" and set all values as in this new query, Just imagine if you had 1000 different records in the table and you mistakenly ran that query, everything would be changed in one execution of that query, that could be dangerous ;)

Anyango

7:35 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hmm Result is wrong..... no problem at all. would u like to say what is the result which you expected and what is the result that you are getting?, so we can look for the reason where it went wrong.

Anyango

7:45 pm on Nov 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh did you mean $result is wrong? you expected to get id of newly created record in result variable?

Twisted Mind

7:50 pm on Nov 8, 2005 (gmt 0)

10+ Year Member



nvm fixed it already :p