SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
-
May 12, 2007, 08:36 #1
- Join Date
- Nov 2005
- Location
- Macedonia
- Posts
- 368
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Simple INSERT Statement - How to make it work only when I click submit?
I'm learning the basics of PHP/mySQL so I'm little stuck at the INSERT stuff.
Basically my code works great, it's just that I want data to be inserted into the database only when I click submit but it shows some error "Error: Query was empty"
here is my PHP code:
PHP Code:if (isset($_POST[submit])){
$insertNews = "INSERT INTO news (title, short_text, long_text)
VALUES ('$_POST[title]', '$_POST[short_text]', '$_POST[long_text]')";
}
if (!mysql_query($insertNews, $connect)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
HTML Code:<form name="news" method="post" action="<?php $_SERVER['php self'] ?>" enctype="multipart/form-data">
Author at GraphicRiver
-
May 12, 2007, 09:42 #2
- Join Date
- Jun 2005
- Posts
- 436
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Use single quotes in your past vars so $_POST[var] becomes $_POST['var'].
I only don't use single quotes when the name of the variable is set to the value of another value, for example:
$variable = "var";
$var = $_POST[$variable];
You're also going to want to prepare the data for the database.
So here is a revised PHP code:
PHP Code:function prepareData($data) {
$data = htmlspecialchars($data);
if (get_magic_quotes_gpc())
$data = stripslashes($data);
return mysql_real_escape_string($data);
}
if (isset($_POST['submit'])){
$title = prepareData($_POST['title']);
$short = prepareData($_POST['short_text']);
$long = prepareData($_POST['long_text']);
$insertNews = "INSERT INTO news (title, short_text, long_text)
VALUES ('$title', '$short', '$long')";
}
if (!mysql_query($insertNews, $connect)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
Code:<form name="news" method="post" action="<?php $_SERVER['php self']; ?>" enctype="multipart/form-data">
Code:<input type="submit" name="submit" value="Post">
-
May 12, 2007, 10:37 #3
- Join Date
- Nov 2005
- Location
- Macedonia
- Posts
- 368
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I used the code that you provided and it still showed "Error: Query was empty" Then I cut-paste the code after the closing </html> tag and it works.
But there is still one thing I want to improve. On the left side I have all the news created and on the right side I have the form for creating new news. When I populate the form, hit submit, the page refreshed the news are created in the database, but then I need to click refresh again in the browser in order the new news to appear on the left site where all my created news are listed. Is it because my code for the insert statement is below the </html> ?Author at GraphicRiver
-
May 12, 2007, 10:45 #4
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
If you SELECT from the database, display the news, then INSERT the new entry, naturally the new entry wasn't in the database when you ran the SELECT.
Anyway, another warning: You should use something other than the submit button to identify the form being submitted. Browsers treat sending the submit button of a form differently. Some older ones never send it as part of the form, newer ones do if you click the button but may not if you press enter while in a form field instead of using the submit button to submit.Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
Bookmarks