-2

我今天从这里更新了我的代码:

$queue = "UPDATE hurlumhei SET barn = $barn, voksenuke = $voksenuke, voksenhelg =     $voksenhelg";

对此

$queue = "UPDATE hurlumhei 
             SET barn = $barn, 
                 voksenuke = $voksenuke, 
                 voksenhelg = $voksenhelg, 
                 klippekort = $klippekort, 
                 klippekortmega = $klippekortmega, 
                 parkering = $parkering, 
                 Kakao = $kakao, 
                 Te = $te,  
                 Kaffe = $kaffe,  
                 Solbærtoddy = $solbærtoddy,  
                 Powerrade = $powerrade,  
                 Brus_stor = $brus_stor,  
                 Brus_medium = $brus_medium,  
                 Brus_liten = $brus_liten,  
                 Bonaqua = $bonaqua,  
                 Iste = $iste,  
                 Sjokolademelk = $sjokolademelk,  
                 Juice = $juice,  
                 Friskus = $friskus,  
                 Slush = $slush,  
                 Pai = $pai,  
                 Calzone = $calzone,  
                 Lasagne = $lasagne,  
                 Buffalo_burger = $buffalo_burger,  
                 Bakt_potet = $bakt_potet,  
                 Pizza = $pizza,  
                 Panini = $panini,  
                 Toast = $toast,  
                 Inngang_pølse1 = $inngang_pølse1,  
                 Inngang_pølse2 = $inngang_pølse2,  
                 Inngang_calzone = $inngang_calzone,  
                 Frukttallerken = $frukttallerken,  
                 Kake = $kake,  
                 Muffins = $muffins,  
                 Popcorn = $popcorn,  
                 Baconchips = $baconchips,  
                 Potetgull = $potetgull,  
                 Baguette_reker = $baguette_reker,  
                 Baguette_kyllingbryst = $baguette_kyllingbryst,  
                 Baguette_ostskinke = $baguette_ostskinke,  
                 Salat_reker = $salat_reker,  
                 Salat_kyllingbryst = $salat_kyllingbryst,  
                 Salat_ostskinke = $salat_ostskinke";

谁能帮我找出错误?对于记录,数据库中的列在新列上的首字母大写,所以这不是错误。欢迎大家提出建议,谢谢

4

1 回答 1

3

您应该考虑为此使用 MySQLi 和准备好的语句,这是一个示例:

$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

// Here we prepare your query and make sure it is alright
// for each field we define it as "fieldname = ?"
$sql = "UPDATE hurlumhei 
         SET barn = ?, 
             voksenuke = ?, 
             voksenhelg = ?, 
             klippekort = ?";
if (!$stmt = $con->prepare($sql))
    die('Query failed: (' . $con->errno . ') ' . $con->error);

// Here we define the field types and the variable that will fill it 
// s stands for string, 
// i for integer, 
// d double and
// b for blob
// for each field you have, you will need 1 letter
// on this example I am using 4 of your fields and 
// considering them as strings so we need to have 4's like this 'ssss'
if (!$stmt->bind_param('ssss', $barn, $voksenuke, $voksenhelg, $klippekort))
    die('Binding parameters failed: (' . $stmt->errno . ') ' . $stmt->error);

// Now we finally execute the data to update it to the database
// and if it fails we will know
if (!$stmt->execute())
    die('Execute failed: (' . $stmt->errno . ') ' . $stmt->error);
else
   echo "Yay we updated something...";

正如您在上面的示例中看到的那样,我定义了每列的变量将使用什么?,稍后bind_param我定义了变量是什么以及它将进入的位置。您可以在此处阅读有关 bind_param 字段类型的更多信息。

您需要正确定义变量的扩展名,{}并将其放在单引号内,以避免由于空格和其他原因而失败。

此外,您正在使用列字段,例如Solbærtoddy我不确定 MySQL 是否接受这些类型的字母。

$queue = "UPDATE hurlumhei 
             SET barn = '{$barn}', 
                 voksenuke = '{$voksenuke}', 
                 voksenhelg = '{$voksenhelg}', 
                 klippekort = '{$klippekort}', 
                 klippekortmega = '{$klippekortmega}', 
                 parkering = '{$parkering}', 
                 Kakao = '{$kakao}', 
                 Te = '{$te}', 
                 Kaffe = '{$kaffe}', 
                 Solbærtoddy = '{$solbærtoddy}', 
                 Powerrade = '{$powerrade}', 
                 Brus_stor = '{$brus_stor}', 
                 Brus_medium = '{$brus_medium}', 
                 Brus_liten = '{$brus_liten}', 
                 Bonaqua = '{$bonaqua}', 
                 Iste = '{$iste}', 
                 Sjokolademelk = '{$sjokolademelk}', 
                 Juice = '{$juice}', 
                 Friskus = '{$friskus}', 
                 Slush = '{$slush}', 
                 Pai = '{$pai}', 
                 Calzone = '{$calzone}', 
                 Lasagne = '{$lasagne}', 
                 Buffalo_burger = '{$buffalo_burger}', 
                 Bakt_potet = '{$bakt_potet}', 
                 Pizza = '{$pizza}', 
                 Panini = '{$panini}', 
                 Toast = '{$toast}', 
                 Inngang_pølse1 = '{$inngang_pølse1}', 
                 Inngang_pølse2 = '{$inngang_pølse2}', 
                 Inngang_calzone = '{$inngang_calzone}', 
                 Frukttallerken = '{$frukttallerken}', 
                 Kake = '{$kake}', 
                 Muffins = '{$muffins}', 
                 Popcorn = '{$popcorn}', 
                 Baconchips = '{$baconchips}', 
                 Potetgull = '{$potetgull}', 
                 Baguette_reker = '{$baguette_reker}', 
                 Baguette_kyllingbryst = '{$baguette_kyllingbryst}', 
                 Baguette_ostskinke = '{$baguette_ostskinke}', 
                 Salat_reker = '{$salat_reker}', 
                 Salat_kyllingbryst = '{$salat_kyllingbryst}', 
                 Salat_ostskinke = '{$salat_ostskinke}'";

您还应该使用mysql_real_escape_string或准备好的语句来避免 SQL 注入。

于 2013-08-22T16:54:11.550 回答