列举往数据库里面添加数据。例子如下
先建个from表单
<form action="addchuli.php" method="post"> <div>水果代号:<input type="text" name="ids" /></div> <div>水果名称:<input type="text" name="name" /></div> <div>水果价格:<input type="text" name="price" /></div> <div>水果产地:<input type="text" name="source" /></div> <div>水果库存:<input type="text" name="numbers" /></div> <div><input type="submit" value="添加" /></div> </form>
注意数据库的名字,还有连接地址别错了
然后写php文件
$ids = $_POST["ids"]; $name = $_POST["name"]; $source = $_POST["source"]; $price = $_POST["price"]; $numbers = $_POST["numbers"]; $db = new MySQLi("localhost","root","123","mydb"); $sql = "insert into fruit values(‘{$ids}‘,‘{$name}‘, {$price},‘{$source}‘,{$numbers},‘‘)"; if($db->query($sql)){ echo "添加成功!"; }else{ echo "添加失败!"; }
刷新数据库就显示添加成功了。
时间: 2024-10-08 16:27:43