gpt4 book ai didi

javascript - 使用 mootools 从 mysql 获取图像 url

转载 作者:行者123 更新时间:2023-11-28 23:56:06 24 4
gpt4 key购买 nike

我正在尝试使用表单搜索 mysql 数据库(在 phpMyAdmin 中创建),结果更新图像。该数据库由 2 列组成,ID(自动递增主键)和图像(图像的 url/路径)。搜索仅检查 ID。

我很确定问题是我没有访问 myRequest.send 的输入参数,也没有返回 onSuccess 期望从 php 代码获得的响应值。

测试.html

<!doctype html>
<html>
<head>
<title>UI Test</title>
<script type="text/javascript" src="MooTools-Core-1.5.1.js"></script>
<link rel="stylesheet" type="text/css" href="res/UI.css"/>
</head>

<body>
<script>
var searchForm = new Element('form', {
id: 'search_form',
method: 'post'
});
var searchText = new Element('input', {
id: 'search_text',
type: 'text',
name: 'name'
});
var searchButton = new Element('input', {
id: 'search_button',
type: 'submit',
name: 'submit',
value: 'Search'
});
var image = new Element('img', {
id: 'image_',
src: 'res/img/default.png',
alt: 'Image',
height: '50',
width: '50',
position: 'relative',
float: 'right'
});

var myRequest = new Request({
url: 'test.php',
method: 'get',
onRequest: function(){
image.set('alt','loading...');
},
onSuccess: function(response){
image.set('alt', response);
image.set('src', response);
},
onFailure: function(){
image.set('src', 'res/img/fail.png');
image.set('alt','failing...');
}
});

searchText.inject(searchForm);
searchButton.inject(searchForm);
searchForm.inject(document.body);
image.inject(document.body);

window.addEvent('domready', function(){
searchButton.addEvent('click', function(event){
event.stop();
myRequest.send('name='+searchText.value);
});
});
</script>
</body>
</html>

测试.php

if(preg_match("/^[  0-9]+/", $_POST['name'])){
$name=$_POST['name'];

//-connect to the database
$db=mysql_connect ("localhost", "root", "password") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("tutorial");
//-query the database table
$sql="SELECT ID FROM `image` WHERE ID LIKE '%" . $name ."%'";

//-run the query against the mysql query function
$result=mysql_query($sql);
if (false === $result) {
echo mysql_error();
}

//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$ID=$row['ID'];
$image=$row['Image'];

//-want to return image path here
echo $image;
}
}
else{

echo "res\img\fail.png";
}

编辑:让 onSuccess 将图像替代文本更改为响应显示第 2 行的名称上存在 undefined index 错误。

EDIT2:进步!以下 php 代码获取适当的图像 url 以设置为替代文本,但图像 src 无法更改(因此显示替代文本)。如何让图像 src 正确更改?

<?php
$name=$_GET['name'];
if(preg_match("/^[ 0-9]+/", $name)){
//-connect to the database
$db=mysql_connect ("localhost", "root", "password") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("tutorial");
//-query the database table
$sql="SELECT Image, ID FROM image WHERE ID LIKE '%" . $name ."%'";

//-run the query against the mysql query function
$result=mysql_query($sql);
if (false === $result) {
echo mysql_error();
}

//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$ID=$row['ID'];
$image=$row['Image'];

//-want to return image path here
echo $image;
}
}
else{

echo "lulz";
}
?>

EDIT3:糟糕,我在图像 src 路径中使用了反斜杠……现在工作正常。

最佳答案

您没有在 SQL 语句中选择“图像”列。

您的代码应该是:$sql="SELECT Image, ID FROM image WHERE ID LIKE '%". $名称."%'";代替$sql="SELECT ID FROM image WHERE ID LIKE '%". $名称."%'";

关于javascript - 使用 mootools 从 mysql 获取图像 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com