我正在生成一个图像列表,并希望根据是否在我的图像旁边按下提交按钮来插入特定图像。
这是我生成图像列表的代码。图像旁边是提交按钮,如计数和其他数据:
// Display results
foreach ($media->data as $data) {
echo "<a href=\"{$data->link}\"</a>";
echo "<h4>Photo by: {$data->user->username}</h6>";
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<h5>Like Count for Photo: {$data->likes->count}</h5>";
echo "<form>";
echo '<input type="submit" onClick=post()>';
echo "</form>";
}
然后我尝试将该图片插入我的数据库:
InstagramImages(DB) - 图片(字段)
function post() {
$hostname = "redacted";
$username = "redacted";
$dbname = "redacted";
//These variable values need to be changed by you before deploying
$password = "redacted";
$usertable = "InstagramImages";
//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
$sql="INSERT INTO $usertable (image) VALUES ('$pictureImage')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}
任何帮助将不胜感激。我需要帮助为php变量$ pictureImage创建正确的INSERT查询。
答案 0 :(得分:0)
另外,<form>
应为<form method='post'>
而不是onclick = post()
将其更改为name='post'
而不是function post(){
使其成为if($_POST['post']){
答案 1 :(得分:0)
echo '<input type="submit" onClick=post()>';
onClick = post() - 这是一个仅用于javascript的javascript事件,但是你用php代码编写的post()函数,因此无法插入到你的tatabase表中。