Dies ist mein Code.PHP Formular sendet keine Daten an MySQL bei der Eingabe
Ich möchte das Formular enthalten, um Daten an eine Tabelle namens "Kontakte" in einer MySQL-Datenbank, die ich erstellt habe, zu senden.
Es gibt vier Felder in der Tabelle; Titel, Name, E-Mail, Anfrage.
Dies ist Code, den ich kopiert und bearbeitet habe, um meiner Website zu entsprechen. Ich bin neu bei .PHP!
<?php
if(isset($_POST['add'])) {
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn) {
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc()) {
$p_title = addslashes ($_POST['p_title']);
$p_name = addslashes ($_POST['p_name']);
$p_email = addslashes ($_POST['p_email']);
}else {
$p_name = $_POST['p_name'];
$p_email = $_POST['p_email'];
}
$p_enquiry = $_POST['p_enquiry'];
$sql = "INSERT INTO contacts ". "(p_name,p_email, p_enquiry, p_title
join_date) ". "VALUES('$p_name','$p_email',$p_enquiry,$p_title NOW())";
mysql_select_db('test_db');
$retval = mysql_query($sql, $conn);
if(! $retval) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border = "0" cellspacing = "1"
cellpadding = "2">
<tr>
<td width = "100"><p>Title:</p></td>
<td><input name = "p_title" type = "text"
id = "p_title"></td>
</tr>
<tr>
<td width = "100"><p>Name:</p></td>
<td><input name = "p_name" type = "text"
id = "p_name"></td>
</tr>
<tr>
<td width = "100"><p>Email:</p></td>
<td><input name = "p_email" type = "text"
id = "p_email"></td>
</tr>
<tr>
<td width = "100"><p>Enquiry:</p></td>
<td><input name = "p_enquiry" type = "text"
id = "p_enquiry"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<input name = "add" type = "submit" id = "add"
value = "Add Employee">
</td>
</tr>
</table>
</form>
<?php
}
?>
Verwenden Sie nicht 'mysql_ *' es wurde entfernt und es wird nicht mehr funktionieren, verwenden Sie 'PDO' [link] (http://php.net/manual/en/book.pdo.php) oder mysqli '[link] (http://php.net/manual/en/book.mysqli.php) stattdessen. – Tom
[Little Bobby] (http://bobby-tables.com/) sagt [Ihr Skript ist für SQL Injection Attacks gefährdet.] (Http://stackoverflow.com/questions/60174/how-can-i-prevent -sql-Injektion-in-php). Sogar [die Zeichenfolge zu entkommen] (http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) ist nicht sicher! –
Bitte [stoppen Sie mit mysql_ * 'Funktionen] (http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [Diese Erweiterungen] (http://php.net/manual/en/migration70.removed-exts-sapis.php) wurden in PHP 7 entfernt. Erfahren Sie mehr [vorbereitet] (http://en.wikipedia.org/ (http://php.net/manual/en/pdo.prepared-statements.php) und [MySQLi] (http://php.net/manual/en/mysqli.quickstart .prepared-statements.php) und erwäge die Verwendung von PDO, [es ist wirklich ziemlich einfach] (http://jayblanchard.net/demystifying_php_pdo.html). –