I'm currently trying to teach myself the basics of PHP and seem to me mucking along alright. I've come across one tutorial that basically describes how to make a blog. I understand every single part of it except how the code is actually firing the request.
The code below basically deals with all the form data accordingly and then posts it to the specified database. Fine. What I don't understand is which bit in that code is actually firing the request to write to the database.
<?php
if (isset($_POST['submit'])) {
$month = htmlspecialchars(strip_tags($_POST['month']));
$date = htmlspecialchars(strip_tags($_POST['date']));
$year = htmlspecialchars(strip_tags($_POST['year']));
$time = htmlspecialchars(strip_tags($_POST['time']));
$title = htmlspecialchars(strip_tags($_POST['title']));
$entry = $_POST['entry'];
$timestamp = strtotime($month . " " . $date . " " . $year . " " . $time);
$entry = nl2br($entry);
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$entry = addslashes($entry);
}
mysql_connect (localhost,GLA,root) ;
mysql_select_db (News);
$sql = "INSERT INTO News (timestamp,title,entry) VALUES ('$timestamp','$title','$entry')";
$result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $s开发者_开发技巧ql . "<br />" . mysql_error());
if ($result != false) {
print "Your entry has successfully been entered into the database.";
}
mysql_close();
}
?>
As far as I can work out, the INSERT INTO ...
section, which would generally do the work, is only being stored as the $result
variable and is not actually being fired. My instinct would tell me to just have an extra line below the line that defines the $result
variable that just simply reads $result
, so as to fire its contents.
However, I know I'm 100% wrong because the script works like a charm!!!
I'm probably missing something very fundamental here but I'd appreciate some sort of explanation!
mysql_query() is the php function that executes the sql statement stored in $sql http://php.net/manual/en/function.mysql-query.php
The $result variable is storing the return of the mysql_query function. That is the function which is doing the database work. Then the $result
variable is simply checked to see if it is not false. If it is false, it means mysql_query
was not able to process the query.
The database insert is being triggered by calling mysql_query($sql) where $sql is an insert statement. mysql_query() isn't only used for reading data from the database, but for all inserts, updates, deletes and even table creation, etc; depending on the value ofthe SQL statement contained in $sql
However, the tutorial where you got this example is pretty dated, and doesn't really ensure that your SQL statement is "safe" to use in a real-world script
Maybe you missing the HTML part which is a form which has
- method which is post
- action which is this script
- a submit button named submit
- and six other input elements named month, date, year, time, title and entry
The whole script fired when the button is pressed (isset($_POST['submit'])
$addedRows = mysql_affected_rows();
$result = mysql_query($sql) or print("Can't insert into table php_blog.<br />" . $sql . "<br />" . mysql_error());
$addedRows = mysql_affected_rows();
if ($addedRows >= 1) {
print "Your entry has successfully been entered into the database.";
}
精彩评论