0

我是 php 新手,认为这应该可以工作,但我不知道我是否在语法上做错了什么。这是处理我从表单提交的数据的脚本。

我将php变量设置为表单中发布的数据,它在变量中回显正确的数据,问题是更新数据库中的记录,它没有......

DB中的表有3个属性,adID、iconURL、webURL。

该表单有 2 个输入文本字段和一个隐藏字段、iconPath、webPath 和 recordN。

<?php
$recNum = $_POST["recordN"];
$iconU = $_POST["iconPath"];
$webU = $_POST["webPath"]; 
echo 'Number of record updated: ' . $recNum;
echo '<br />New Icon Path: ' . $iconU;
echo '<br />New Web Path: ' . $webU;

$con = mysql_connect("localhost","admin","pass");
if (!$con)  {   die('Could not connect: ' . mysql_error());   }

mysql_select_db("DBNAME", $con);

mysql_query("UPDATE adSources set iconURL = $iconU, webURL = $webU 
WHERE adID = $recNum");

mysql_close($con);

echo '<br /><a href="http://mydomain.com/thePage.html" target="_blank">Return to main page</a>' . "\n"; 
?> 

所以在 adID = recNum 的地方,我想覆盖 iconURL = $iconU 和 webURL = $webU

我在 $iconU 和 $webU 中有我想要使用的值,iconURL 和 webURL 是数据库中字段的名称。我很好地阅读了它们并且可以通过这些名称显示它们就好了,我只是无法更新它。

我在数据库中为用户授予了完全权限以及所有这些权限。

有什么帮助吗?与其他语言相比,Php 看起来非常丑陋。

4

2 回答 2

1

您错过了变量名中的引号。尝试:

mysql_query("UPDATE adSources set iconURL = '$iconU', webURL = '$webU'
            WHERE adID = $recNum");

如果您还可以转义变量,那就更好了:

mysql_query("UPDATE adSources set iconURL = '".mysql_real_escape_string($iconU).
            "', webURL = '".mysql_real_escape_string($webU).
            "' WHERE adID = $recNum");

这使它好一点,但mysql_real_escape_string()实际上不鼓励使用。PDO一旦掌握了它,您应该检查并替换它。

于 2012-07-26T03:58:43.157 回答
1

要调试,请使用 mysql_error()。您的语法会出错,因为您需要将字符串($iconU 等)用引号括起来。

BUT... you'll be much better off learning PDO; this will handle the wrapping and making safe of variables for you. mysql functions are being depreciated, so don't start with the old stuff, start with the new! http://php.net/manual/en/book.pdo.php

$sth = $dbh->prepare('UPDATE adSources set iconURL = :iconU, webURL = :webU WHERE adID = :recNum');
$sth->bindValue(':iconU', $iconU , PDO::PARAM_STR);
$sth->bindValue(':webU', $webU , PDO::PARAM_STR);
$sth->bindValue(':recNum', $recNum , PDO::PARAM_INT);
$sth->execute();

Edit: responding to your comment about "freaking ugly". Not intending to start a debate, but yes, it starts ugly. Then you wrap this up into a nice class, get your error handling right (wrap in try/ctach) and you're laughing. So Start with the ugly and you'll soon learn how to clean it up.

于 2012-07-26T04:00:53.253 回答