我的MySQL更新代码出了什么问题?

时间:2013-08-22 16:46:34

标签: php

我今天更新了我的代码:

$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";

任何人都可以帮我找到错误吗?对于记录,数据库中的列在新的列上具有大写首字母,因此这不是错误。欢迎任何建议,谢谢

1 个答案:

答案 0 :(得分: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上定义变量是什么以及它将进入的位置。的 You can read more about the field types of bind_param here.

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

此外,您还在使用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注入。