我是PHP和mysql的新手。我创建了一个接受用户输入的表单,我希望使用存储过程将这些输入插入到mysql中。
Hurray我得到了这个,现在表单数据正在使用存储过程插入到mysql中。
dbconn.php
<?php
// Set connection variables.
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
// Connect to mysql server
$mysqli = new mysqli($host,$user,$pwd,$db);
/* Check if any error occured */
if (mysqli_connect_errno())
{
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit;
}
?>
Add.php
<?php
// If the form was submitted.
if($_POST)
{
// Connect to db.
include 'dbconn.php';
// Retrieve data from form.
$srn = $_POST['srn'];
$client = $_POST['client'];
$type = $_POST['type'];
$fy = $_POST['fy'];
$category = $_POST['category'];
$sd = $_POST['sd'];
$fd = $_POST['fd'];
$assignto = $_POST['assignto'];
$edoc = $_POST['edoc'];
$current = date('m/d/Y');
$date = strtotime("+".$edoc." days", strtotime($current));
$ecd = date("Y/m/d", $date);
$reviewed = $_POST['reviewed'];
$path = $_POST['path'];
// Stored procedure code
if(!$mysqli->query("DROP PROCEDURE IF EXISTS addproc")
|| !$mysqli->query("CREATE PROCEDURE addproc
(IN client varchar(50), IN type varchar(30), IN fy INT, IN category varchar(30), IN sd varchar(80),
IN fd varchar(80), IN assignto varchar(50), IN edoc date, IN reviewed varchar(40), IN upload varchar(100))
BEGIN
INSERT INTO main(client, type, fy, category, sd, fd, assignto, edoc, reviewed, upload) VALUES(client, type, fy, category, sd, fd, assignto, edoc, reviewed, upload);
END; "))
{
echo "Stored procedure creation failed : (" .$mysqli->errno .") " .$mysqli->error;
}
if(!$mysqli->query("CALL addproc('$client','$type','$fy','$category','$sd','$fd','$assignto','$ecd','$reviewed','$path')"))
{
echo "CALL failed : (" .$mysqli->errno .")" .$mysqli->error;
}
else
{
?>
<script language="javascript">
alert("Task created successfully" + "\n\n" + " <?php echo "Service Request Number : " .$srn ?>");
top.location.href = "Home.php"; // Page redirection.
</script>
<?php
}
if(!$res = $mysqli->query("SELECT * FROM main"))
{
echo "SELECT failed : (" .$mysqli->errno .")" .$mysqli->error;
}
}
?>
答案 0 :(得分:-1)
$query = "INSERT INTO main(client, type, fy, category, sd, fd, assignto, edoc, reviewed, upload) VALUES('$client', '$type', '$fy', '$category', '$sd', '$fd', '$assignto', '$edoc', '$reviewed', '$upload')";
if(mysqli_query($mysqli,$query))
&安培;其他