我现在已经做了一段时间了,并且无法弄清楚我做错了什么。
这个脚本看起来很简单,并且正在将文件添加到服务器,但它没有将位置插入数据库。我在哪里使用代码的INSERT部分失败了?
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="photo-upload2.php?district_id=<?php echo $_REQUEST['district_id']?>" enctype="multipart/form-data">
<p>
Please Enter the Candidate's Name.
</p>
<p>
Candidate's Name:
</p>
<input type="text" name="nameMember"/>
<p>
Please Upload a Photo of the Candidate in gif or jpeg format. The file name should be named after the Candidate's name.<br>
If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb. </p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="photo">
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the Database" value="Add Pic"/>
</form>
</body>
</html>
<?php
// This is the directory where images will be saved
$target = "../images/candidates/";
$target = $target . basename( $_FILES['photo']['name']);
// This gets all the other information from the form
$name=$_POST['nameMember'];
$district_id=$_REQUEST['district_num'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
include('db.php');
//Writes the information to the database
$sql = "INSERT INTO candidate_images SET
name='$name',
image='$pic,
district_id='$district_id'";
// Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
// Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else { // if name is blank
if ($name=='') { echo "Please enter Candidate's name.";
// Gives and error if it's not ok
} else echo "Sorry, there was a problem uploading your file $name.";}
?>
答案 0 :(得分:1)
插入查询错误,请将查询更改为以下内容:
$sql = "INSERT INTO candidate_images (name,image,district_id) VALUES ('$name','$pic','$district_id')";
mysqli_query($connection, $sql);
有关详情,请参阅:mysqli_query
这里我举例说明如何插入数据库:
<?php
// Database connection establishment
$con=mysqli_connect("localhost","username","password","db_name");
// Check connection
if (mysqli_connect_errno($con)) {
echo "MySQL database connection failed: " . mysqli_connect_error();
}
$sql = "INSERT INTO candidate_images (name,image,district_id) VALUES ('$name','$pic','$district_id')";
// Insert data into database
mysqli_query($con,$sql);
?>
答案 1 :(得分:0)
$sql = "INSERT INTO candidate_images (`name`,`image`, `district_id`) VALUES ('$name','$pic','$district_id')";
在此处使用mysql_*
OR mysqli_*
或PDO
函数对数据库执行查询。
答案 2 :(得分:0)
实际上你的插入查询是错误的。你只需要纠正它。它会工作正常。 只需用以下代码替换您的代码,不要忘记执行保存在$ sql变量中的查询。这也是必要的。如果我的工作不起作用,请使用您自己的类/模块来执行查询
while(input.hasNext()) {
if(input.hasNextInt()) {
x = input.nextInt();
if(x <= 0 || x > 3) {
// throw exception or whatever your requirements is.
}
switch(x){
case 1:
System.out.println("You're in Gear 1");
break;
case 2:
System.out.println("Gear 2");
break;
case 3:
System.out.println("Gear3");
}
}
}