下午的天才,希望你能帮忙。这是在我添加上传图片之前插入到表 Twitch 和 Birds 中的,现在唯一有效的部分是图片上传。这是非常简单的代码,但无法弄清楚为什么它不添加。
另外,一旦 TWITCH 表工作,我如何从 TWITCH 表中获取自动增量 ID。提前致谢。
<?php
session_start();
include ('dbconnect.php');
//Set Variables from Form
$species =($_POST['Species']);
$age =($_POST['Age']);
$sex =($_POST['Sex']);
$location =($_POST['Location']);
$date_seen = date("Y-m-d");
$time_seen =($_POST['Time']);
$twitch =($_POST['Comments']);
//$twitch_id=(". mysql_insert_id()");
//Insert Into Birds table
$query1 = "INSERT INTO Birds (Species, Age, Sex, Location, Date_Seen, Time_Seen, Comments) VALUES
( '$species', '$age', '$sex', '$location', '$date_seen', '$time_seen', '$twitch')";
result1 = mysqli_query($conn, $query1);
//Insert into Twitch table
query2 = "INSERT INTO Twitch (Twitch_ID, Twitch) VALUES( 'NULL', '$twitch')";
$result2 = mysqli_query($conn, $query2);
// Validate and upload image file
if ( !preg_match( '/gif|png|x-png|jpeg/', $_FILES['userFile']['type']) ) {
die('<p>Only browser compatible images allowed</p></body></html>');
} else if ( strlen($_POST['altText']) < 9 ) {
die('<p>Please provide meaningful alternate text</p></body></html>');
} else if ( $_FILES['userFile']['size'] > 1116384 ) {
die('<p>Sorry file too large</p></body></html>');
// Connect to database
} else if ( !($conn=mysqli_connect($host, $username, $password)) ) {
die('<p>Error connecting to database</p></body></html>');
} else if ( !(mysqli_select_db($conn, $db_name)) ) {
die('<p>Error selecting database</p></body></html>');
// Copy image file into a variable
} else if ( !($handle = fopen ($_FILES['userFile']['tmp_name'], "r")) ) {
die('<p>Error opening temp file</p></body></html>');
} else if ( !($image = fread ($handle, filesize($_FILES['userFile']['tmp_name']))) ) {
die('<p>Error reading temp file</p></body></html>');
} else {
fclose ($handle);
// Commit image to the database
$image = mysqli_real_escape_string($conn, $image);
$alt = htmlentities($_POST['altText']);
$query3 = 'INSERT INTO Image (Image_Name, Image_Type, Description, Image) VALUES ("' . $_FILES['userFile']['type'] . '","' . $_FILES['userFile']['name'] . '","' . $alt . '","' . $image . '")';
if ( !(mysqli_query($conn, $query3)) ) {
die('<p>Error writing image to database</p></body></html>');
} else {
header ("Location: TwitchWall.php");
}
}
?>