提问人:tim de hart 提问时间:8/22/2015 最后编辑:tim de hart 更新时间:8/22/2015 访问量:108
Php Undifend 索引 [重复]
Php Undifend index [duplicate]
问:
我以前用过这段代码。现在我对其他脚本使用了相同的代码,但现在我收到了通知。我想把这些信息放在数据库中,但我总是得到未定义的索引。不管我做什么。
<pre><code>
<?php
define('DB_NAME', 'videonaardvd');
define('DB_USER', 'root');
define('DB_PASSWORD', 'usbw');
define('DB_HOST', 'localhost');
$Type = $_POST['Type'];
$Aantal = $_POST['Aantal'];
$ExtraKopie = $_POST['ExtraKopie'];
$Naam = $_POST['Naam'];
$Adres = $_POST['Adres'];
$Postcode = $_POST['Postcode'];
$Plaats = $_POST['Plaats'];
$Telefoon = $_POST['Telefoon'];
$Email = $_POST['Email'];
$Datum = $_POST['Datum'];
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link){
die('could not connect:' . mysql_error());
}
$db_selected = mysql_select_db (DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error ());
}
$sql = "INSERT INTO gegevens (Type,Aantal,ExtraKopie,Naam,Adres,Postcode,Plaats,Telefoon,Email,Datum)
VALUES ('$Type','$Aantal','$ExtraKopie','$Naam','$Adres','$Postcode','$Plaats','$Telefoon','$Email','$Datum'";
if(!mysql_query($sql)){
die('Er is iets fout gegaan. Probeer het later opnieuw.: '. mysql_error());
}
?>
Bedankt voor het versturen van je opdracht!!!
<br />
<input class="terug" type=button onClick="location.href='index.html'" value='Terug'>
--------------------------- html ----------------------------------------
<fieldset>
<div id="type">
<form><div id="namen"> <h4 class="namen"> Type...........................Aantal...............Extra kopie</h4> </div>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type" >VH / S-VHS <br>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type">Video2000 / VCC <br>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type">Mini DV <br>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type">Video 8, HI8 & Digital 8 <br>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type">Betamax <br>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type">VHS-C <br>
<input type="text" class="aantal" name="Aantal"> <input type="checkbox" class="extra" name="ExtraKopie"> <input type="checkbox" name="Type">Filmspoelen <br>
</form>
</div>
<div id="gegevens">
<form>
<input type="text" name="Naam" placeholder="De heer/mevrouw"><br>
<input type="text" name="Adres" placeholder="Adres"><br>
<input type="text" name="Postcode" placeholder="Postcode">
<input type="text" name="Plaats" placeholder="Plaats"><br>
<input type="text" name="Telefoon" placeholder="Telefoon"><br>
<input type="text" name="Email" placeholder="Email"><br> <br><br>
<input type="text" name="Datum" placeholder="Datum:"><br>
</form>
</div>
<input type="submit" href="collect.php" class="versturen">
</fieldset>
答:
0赞
Joshua Nightingale
8/22/2015
#1
没有尝试过这个,所以我会让你试一试。
$user = 'root';
$pass = 'usbw';
$dbname = 'videonaardvd';
$host = 'localhost';
$opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
$dsn = "mysql:host=$host;dbname=$dbname";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
function died($error){
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if (!$_POST['Type'] || !$_POST['Aantal'] || !$_POST['ExtraKopie'] || !$_POST['Naam'] || !$_POST['Adres'] || !$_POST['Postcode'] || !$_POST['Plaats'] || !$_POST['Telefoon'] || !$_POST['Email'] || !$_POST['Datum']) {
died('<p>Please remember to supply all of the requested information! You may press your back button to attempt again!</p>');
exit;
}
$Type = $_POST['Type'];
$Aantal = $_POST['Aantal'];
$ExtraKopie = $_POST['ExtraKopie'];
$Naam = $_POST['Naam'];
$Adres = $_POST['Adres'];
$Postcode = $_POST['Postcode'];
$Plaats = $_POST['Plaats'];
$Telefoon = $_POST['Telefoon'];
$Email = $_POST['Email'];
$Datum = $_POST['Datum'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$Email)){
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$Naam)){
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0){
died($error_message);
}
else {
try {
$DBH = new PDO($dsn, $user, $pass, $opt);
$STH = $DBH->prepare("INSERT INTO gegevens (Type,Aantal,ExtraKopie,Naam,Adres,Postcode,Plaats,Telefoon,Email,Datum) VALUES (:Type, :Aantal, :ExtraKopie, :Naam, :Adres, :Postcode, :Plaats, :Telefoon, :Email, :Datum)");
$STH->bindParam(':Type', $_POST['Type']);
$STH->bindParam(':Aantal', $_POST['Aantal']);
$STH->bindParam(':ExtraKopie', $_POST['ExtraKopie']);
$STH->bindParam(':Naam', $_POST['Naam']);
$STH->bindParam(':Adres', $_POST['Adres']);
$STH->bindParam(':Postcode', $_POST['Postcode']);
$STH->bindParam(':Plaats', $_POST['Plaats']);
$STH->bindParam(':Telefoon', $_POST['Telefoon']);
$STH->bindParam(':Email', $_POST['Email']);
$STH->bindParam(':Datum', $_POST['Datum']);
$STH->execute();
} catch (PDOException $e) {
echo $e->getMessage();
}
echo "<p>Submitted!</p>";
}
}
$DBH = null;
评论
0赞
tim de hart
8/22/2015
试一试。我现在还有 2 个通知:。未定义的变量:第 7 行的主机。未定义的索引:在第 16 行键入。
0赞
Joshua Nightingale
8/22/2015
@timdehart对于第 7 行,需要在“$dbname = 'videonarrdvd'下添加;更正了上述代码中的此部分。至于第 16 行,请检查表单并查看您的输入之一中是否调用了 Type,或者向我展示您的 html 表单。$host = 'localhost';
0赞
tim de hart
8/22/2015
不知道会出什么问题。一切都打得很好。我已将html放在php脚本下。也许你看到了什么
0赞
Joshua Nightingale
8/22/2015
你有一堆名称为“Type”的复选框,你可以使用 value=“CHECKBOXNAME” 并添加它。查询正在查看许多类型或类似的东西 http://stackoverflow.com/questions/23292983/how-insert-the-multiple-checkbox-values-into-database-using-php
评论