我正在创建一个将新项目添加到目录(数组)中的脚本。每次我按下提交它创建新数组并且旧记录消失。我使用数字作为数组键,所以他们不会相互嘲笑和覆盖,但它仍然无法工作。 我想要的是:保存数组中的每条记录。
<!DOCTYPE HTML>
<html>
<body>
<?php
if(isset($_POST['Submit'])){
$catalog[$_POST['number']]=array("name" => $_POST['name'], "price" => $_POST['price'], "make" => $_POST['make']);
foreach
( $catalog as $var ) {
echo "\n", "<b>Name:</b> ", $var['name'], "\t\t", " // <b>Price:</b> ", $var['price'], "\t\t", " // <b>Make:</b> ", $var['make'];
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Number:<br>
<input type="text" name="number" value="<?php
end($catalog);
$key = key($catalog);
echo $key+1;
?>"><br>
Name:<br>
<input type="text" name="name"><br>
Price:<br>
<input type="text" name="price"><br>
Make:<br>
<input type="text" name="make"><br><br>
<input type="submit" value="Submit" name="Submit">
</form>
</body>
</html>
答案 0 :(得分:0)
<?php
error_reporting(0);
session_start();
$catalog=array();
$catalog=$_SESSION['catalog'];
if(isset($_POST['Submit'])){
$catalog[$_POST['number']]=array("name" => $_POST['name'], "price" => $_POST['price'], "make" => $_POST['make']);
$_SESSION['catalog'][]=$catalog[$_POST['number']];
}
echo "<pre>"; print_r($_SESSION); echo "</pre>";
?>
<!DOCTYPE HTML>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
Number:<br>
<input type="text" name="number" value="<?php echo count($catalog)+1 ?>"><br>
Name:<br>
<input type="text" name="name"><br>
Price:<br>
<input type="text" name="price"><br>
Make:<br>
<input type="text" name="make"><br><br>
<input type="submit" value="Submit" name="Submit">
</form>
</body>
</html>