我已经定义了变量 $row_id1(例如 $row_id1 = 1;)
在 mysql 中是名为 Number 的列。
想要选择列 Number 并且如果列中存在 $row_id,想要获取(获取、定义)该值。实际上想检查列 Number 中是否存在 $row_id 值。
代码有什么问题?
$stmt = $db->prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1");
$stmt->bindParam(':Number1', $row_id1); //value from $_POST
$stmt->execute();
$Number1 = $stmt->fetchAll(PDO::FETCH_ASSOC); // seems here something is wrong
echo $Number1 .' $Number1<br>';
在输出中我得到 Array $Number1
好像简单(愚蠢)的问题,但搜索了几个小时却没有解决方案
++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
更新代码
此部分与问题无关(仅供参考)
require($_SERVER['DOCUMENT_ROOT'] . "/_additionally_protected/session.class.php");
$session = new session();
// Set to true if using https
$session->start_session('_a', false);
session_regenerate_id();
//require($_SERVER['DOCUMENT_ROOT'].'/_additionally_protected/request_blocker.php'); //slow page load
require($_SERVER['DOCUMENT_ROOT'].'/_measure_time_start.php');
require($_SERVER['DOCUMENT_ROOT'].'/_additionally_protected/db_config.php');
header('Content-type: text/html; charset=utf8');
require($_SERVER['DOCUMENT_ROOT'] . "/only_private/blackhole.php");
ini_set('session.bug_compat_warn', 0);
ini_set('session.bug_compat_42', 0);
这与问题有关(这里我从输入中获取值)
if(get_magic_quotes_gpc())
$row_id1 = htmlspecialchars(stripslashes($_POST['row_id1']));
else
$row_id1 = htmlspecialchars($_POST['row_id1']);
echo $row_id1 .' row_id1 from $_POST<br>';
连接到 mysql
try {
$db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass//, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//echo "Connected to database"; // check for connection
}
catch(PDOException $ex) {
//echo "An Error occured!"; //user friendly message
//print "Error!: " . $ex->getMessage() . "<br/>";
//some_logging_function($ex->getMessage());
exit;
}
在这里,我从 mysql 获取最后一行编号。稍后我将数字+1 传递给 html 隐藏的 iput 字段。我的意思是在页面加载时我得到最后一个数字。如果用户单击按钮不执行代码。
//fetch last Number to pass to row_id and entry_id
if( !$_POST['register'] ) {
echo '!$_POST[register] <br>';
//+++++++++++++ now multiple PDO
// Select table with query
$stmt = $db->query("
SELECT Number FROM 2_1_journal ORDER BY Number DESC LIMIT 1
");
// Set fetching mode
$stmt->setFetchMode(PDO::FETCH_ASSOC);
// Assign $row as your key to access Table fields
foreach ($stmt as $row) :
echo $row['Number'] .' $row[Number] On page load (reload) get number of last row in mysql; to pass to Entry ID and rowid<br>';
$_SESSION['last_number_from_mysql'] = $row['Number'];
endforeach;
}//if( !$_POST['register'] ) {
会议(上)
这是对我造成问题的部分 // 从数据库中获取数字以与行 ID(隐藏)进行比较。旨在决定是否需要记录新行或是否更新现有行。代码在 $_POST 上执行。所以必须定义 $row_id (上面定义)
if( $_POST['register'] ) {
如果用户只点击按钮然后执行代码
echo $row_id1 .' row_id1 before select Number where Number is $row_id1<br>';
这是要检查的。我看到 row_id1 号码;这意味着该号码已收到。就在下面想用它
$stmt = $db->prepare("SELECT Number FROM 2_1_journal WHERE Number = :Number1");
$stmt->bindParam(':Number1', $row_id1);
$stmt->execute();
echo $Number1 = $stmt->fetchColumn() .' $Number1<br>';
仅当两次单击按钮时才会回显
这是html输入
<input type="tex" name="row_id1" id="row_id1" value="
<?php
//if( ($_POST['register']) === 'Save draft' ) {
//echo $_POST['row_id1'];
//}
//else {
echo ($_SESSION['last_number_from_mysql'] + 1);
//}
?>
">