2017-04-10 81 views
-3

我在寻找为什么我的mysql不起作用,但在这种情况下没有找到任何帮助我的东西。PHP值没有通过更新查询

我的问题是,我想要update我的数据库通过在$_GET中输入一个具体的ID来记录日期。这里看着:

$date = date('Y-m-d', time()); 

session_start(); 
header('content-type: text/html; charset=utf-8'); 
require_once("cnx_db_inventory.php"); 
$idComputer=$_GET['idComputer']; 
$query="select computer.computer_id, 
    computer.computer_name 
    from computer 
    where computer.computer_id='$idComputer'"; 
$data = get_array($query); 
foreach($data as $value) { 
    $computername=$value['computer_name']; 
} 

//Update the database 
if(isset($_GET['validate'])){ 
    $idComputer; 
    $query="UPDATE computer 
    SET deleted_date='$date' 
    WHERE computer.computer_id=$idComputer"; 
    mysql_query($query); 
    echo $query; 
} 

我的我的查询echo看起来像这样去验证:

UPDATE computer SET deleted_date='2017-04-07' WHERE computer.computer_id= 

如果我把一些引号(”“),以$idComputer,回声给了我这个:

UPDATE computer SET deleted_date='2017-04-07' WHERE computer.computer_id=” 

但是,如果我在isset()之外回复(参见here),它给了我id。 为什么然后,当我点击验证时,computer_id没有通过?

我的形式是这样的:

<form id="general" name="general" action="delete.php" method="get"> 
    Are you sure you want to delete this?<br /> 
    <strong><font size="2" color ="black">Computer name </font></strong> 
    <input type="text" id="computername" name="computername" value="<?php echo $computername;?>"/><br /> 
    <input type="submit" name="validate" value="Validate" class="button"> 
    <input type="submit" name="cancel" class="button" value="Cancel" onClick="general.action='researchInventory.php'; return true;"/> 
</form> 

我尝试把$IdComputer在isset,但不会改变任何东西。我之前有过类似的问题,这有助于我解决问题,但在这种情况下不能。我尝试通过if(){}之外的mysql_query。我尝试在SELECT中拨打computer.deleted_date,但什么也没有。

我试图把一个隐藏的输入值为computer_id,尝试$_post方法(看这个one),但没有任何变化,当我验证!无论如何,computer_id都没有通过。

class DBConnection{ 
private static $_singleton; 
private $_connection; 
private function __construct(){ 
    $ip =$_SERVER['REMOTE_ADDR']; 
    $this->_connection = @mysqli_connect(DB_HOST, DB_USER, DB_PASS,DB_NAME) or die("Could not connect to database"); 
mysqli_set_charset($this->_connection,"utf8"); 
} 
public static function getInstance(){ 
    $ip =$_SERVER['REMOTE_ADDR']; 
    if (is_null (self::$_singleton)) { 
     self::$_singleton = new DBConnection(); 
      mysqli_set_charset('utf8');//Line 20; 
    } 
     return self::$_singleton; 
    } 
    public function getHandle(){ 
     return $this->_connection; 
    } 
} 
+1

'$ data = get_array($ query);'here? – JustOnUnderMillions

+4

请不要使用'mysql_ *'功能。它在PHP5中被弃用,并在PHP7中被删除。改用MySQLi或PDO。 – Jer

+0

给我'数组' – Nexis

回答

0

工作例如:添加POST方法,只是因为我不喜欢得到这么多的:),并与ID

<?php 

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

include"config.inc.php"; // only DB parameters in here 

$mysqli = mysqli_connect("$host", "$user", "$mdp", "$db"); 

if (mysqli_connect_errno()) { echo "Error connecting : " . mysqli_connect_error($mysqli); } 

$id = $_GET['id']; /* assuming that your reach this page with an URL parameter ?id=3 ou any number */ 
echo"[ 1st check on ID -> $id ]"; 

// TODO -> clean var ID and check if set/numeric 

if(isset($id)) { 

/* prepare select */ 

$query = " SELECT computer_id, computer_name FROM computer WHERE computer_id=? "; 
$stmt = $mysqli->prepare($query); 

$stmt->bind_param("s", $id); /* if integer ID -> 's' becomes 'i' */ 

$results = $stmt->execute(); 
$stmt->bind_result($computer_id, $computer_name); 
$stmt->store_result(); 

if ($stmt->num_rows > 0) { 

while($stmt->fetch()){ 
echo"[ $computer_id -> $computer_name ]<br />"; 
} 
} 
else { echo"[ no data ]"; } 

//$stmt->free_result(); 

} else { echo"[ no ID defined ! ]"; } 

// Update the database 
if(isset($_POST['validate'])){ 

$date = date('Y-m-d', time()); 
$idComp = $_POST['computerID']; 

/* prepare update */ 

$query1 = " UPDATE computer SET deleted_date=? WHERE computer_id=? "; 
$stmt1 = $mysqli->prepare($query1); 

$stmt1->bind_param("ss", $date, $idComp); /* if integer ID -> 'ss' becomes 'si' */ 

if (!$stmt1->execute()) { echo"false -> update failed"; echo $stmt1->error; } else { echo"true -> update ok"; } 

$stmt1->free_result(); 
} 

?> 

<form id="general" name="general" action="delete.php" method="post"> 
<input type="hidden" id="computerID" name="computerID" value="<?php echo"$computer_id"; ?>" /> 
Are you sure you want to delete this ?<br /> 
<strong>Computer name</strong> 
<input type="text" id="computername" name="computername" value="<?php echo"$computer_name"; ?>" /><br /> 
<input type="submit" name="validate" value="Validate" class="button" /> 
</form> 

隐藏字段编辑:由于mysql_ *已被弃用在PHP 5.5(请参阅PHP doc),你应该更喜欢PPS : Prepared Parameterized Statements。这将有助于Preventing SQL injection

+0

关于执行小问题:我们可以通过一个多值(现在只有'$ date')......像如果我想通过其他科隆名称编号的号码?我应该像这样:$ number =“1”and'$ query1 =“UPDATE computer SET deleted_date = ?, number =?WHERE computer_id =?”;' '$ stmt1-> bind_param(“ss”,$ date,$号码,$ idComp);'? – Nexis

+0

任何参数设置?在查询必须然后bind_param添加,如有需要尽快 – OldPadawan

+0

我肯定会告诉你一个例子,因为我尝试它,并给我一个错误=警告:mysqli_stmt :: bind_param():在类型定义字符串元素的数量在/var/www/html/tickets/testMel/delete.php线不符合绑定变量的数量82假 - >更新failedNo在准备好的声明......这就是我所做的$ QUERY1 =“更新参数提供的数据计算机SET deleted_date = ?, hidden =?WHERE computer_id =?“; $ stmt1-> bind_param( “SS”,$日期,$隐藏,$ idComp);/*如果整数ID - > 'SS' 变成 'SI' */ – Nexis

-1
$idComputer=$_GET['idComputer']; 
$query="select computer.computer_id, 
    computer.computer_name 
    from computer 
    where computer.computer_id='$idComputer'"; 

. 
. 
. 

$query="UPDATE computer 
    SET deleted_date='$date' 
    WHERE computer.computer_id=$idComputer"; 

是computer.computer_id一个字符串类型的列?如果是这样,那么更新语句中的值必须用单引号引起来。

此外,这是非常糟糕的做法。有人可以访问http://yoursite.com/yourscript.php?idComputer=Boston';DROP TABLE computer;--,真的把你搞乱了。你应该使用准备好的语句,或者至少使用mysql_escape_string()函数。

同样的想法也适用于您的HTML输出。

<input type="text" id="computername" name="computername" value="<?php echo $computername;?>"/><br /> 

这应该是这样的

<input type="text" id="computername" name="computername" value="<?php echo htmlspecialchars($computername); ?>"/><br />