PHP $_POST 被存储为单词“数组”


PHP $_POST gets stored as the word 'Array'

我在存储表单中的输入数据时遇到问题。存储在数据库中的只是单词"Array"。似乎我必须对帖子数据做一些事情才能正确存储。无法使其正常工作。任何帮助都值得赞赏。下面是处理输入表单的代码。

<?php
session_start();
/* variables */
$a1 = $_POST['value1'];
$a2 = $_POST['value2'];
$a3 = $_SESSION['user_id'];
/* connect */
$mysqli = new mysqli("xxxx","xxxx","xxxx","xxxx");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s'n", mysqli_connect_error());
    exit();
}
/* prepare statement */
if (!($stmt = $mysqli->prepare("UPDATE table1 SET column1 =? WHERE user_id=? "))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
if (!($stmt2 = $mysqli->prepare("UPDATE table1 SET column2 =? WHERE user_id=? "))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
/* bind parameters for markers */
if (!$stmt->bind_param("ss", $a1, $a3)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt2->bind_param("ss", $a2, $a3)) {
    echo "Binding parameters failed: (" . $stmt2->errno . ") " . $stmt2->error;
}
/* loop and execute */
foreach($a1 as $key => $value){
            $key++;     
        if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }
foreach($a2 as $key => $value){
            $key++;     
        if (!$stmt2->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }
$stmt->close();
$stmt2->close();

想知道变量是否包含数据:

/* show data from the post*/
echo "<pre>";
var_dump($a1);
echo "</pre><br>";
echo "<pre>";
var_dump($a2);
echo "</pre><br>";

事实证明,它与输入匹配。输出:

 array(30) {
  [0]=>
  string(1) "2"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "1"
  etc..
 }
 array(30) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "3"
  etc...
 }

它所做的只是将单词"数组"存储在应该有输入值的地方。谁能指出我正确的方向?我被困住了,真的可以使用一些帮助。

编辑:这是一个关于足球比赛的简单网站。爱好项目:D值 1 是主队的进球数,值 2 是客队的进球数。_POST 美元是输入特定用户在几场比赛中为两支球队进球的输入。

Edit2:从发送来源和使用的表中添加了代码:

//....connection, select query, etc....
$result=mysqli_query($mysqli,$sql);
while($row = $result->fetch_array())
{
$rows[] = $row;
}
?>
//....form and tabel stuff....
    <form action="send1.php" method="post"> 
        <div class="table-responsive">  
            <table class="table">
                <thead>
                    <tr>
                        <th class="col-md-2">Date</th>
                        <th class="col-sm-1">Time</th>
                        <th class="col-md-2">Home - Away</th>
                        <th class="col-sm-1">Home</th>
                        <th class="col-sm-1">Away</th>
                    </tr>
                </thead>
            <tbody>                                                                     
<?php
foreach($rows as $row){
?>                          
                    <tr>
                        <td class="col-md-2"><?php echo $row['date']; ?></td>
                        <td class="col-sm-1"><?php echo $row['time']; ?></td>
                        <td class="col-md-2">
                            <span class="hidden-xs"><?php echo $row['Team_Home']; ?> - <?php echo $row['Team_Away']; ?></span>
                            <span class="visible-xs-block">test1 - test2</span>
                        </td>                                           
                        <td class="col-sm-1"><input type="text" name="score1[]" style="height:20px; width:32px"></td>   
                        <td class="col-sm-1"><input type="text" name="score2[]" style="height:20px; width:32px"></td>                                       
                    </tr>                                   
<?php  
}  
$result->close();
?>  
//....more table stuff with submit button...
<?php
mysqli_close();
?>

输出类似于 10+ 行,输入主队和客队的进球。

数据来源的表如下所示:

  • 比赛
    • Match_id
    • 日期
    • 时间
    • 城市
    • 体育场
    • Team_home
    • Team_away
    • Score_home
    • Score_away

目前预测的位置如下所示:

  • 预测
    • Prediction_id
    • Match_id
    • User_id
    • Score_predict_home
    • Score_predict_away

所以我想要一个表中的所有匹配数据,另一个表中的预测。

编辑3:这就是我现在所做的。不再有 php 错误:D数据在数组中,但最终不会出现在数据库:(

$acMatchPredictions = !empty($_POST['match_prediction']) &&       
is_array($_POST['match_prediction']) ? $_POST['match_prediction'] : array();
foreach($acMatchPredictions as $wedstrijd_id => $aPredictedScores) {
    $score_predict_home = (int) $aPredictedScores['score_predict_home'];
    $score_predict_away = (int) $aPredictedScores['score_predict_away'];
    $user_id = $_SESSION['user_id'];
$query = ('update predictions set score_predict_home=?, score_predict_away=? where match_id=? and user_id=?');
$stmt = $mysqli->prepare($query); 
$stmt->bind_param('iiii', $match_id, $user_id, $score_predict_home, $score_predict_away);
$stmt->execute();
}
$stmt->close();
echo "<pre>";
var_dump($acMatchPredictions);
echo "</pre><br>";

var_dump的输出:

array(72) {
  [0]=>
  array(1) {
    ["score_predict_home"]=>
    string(1) "1"
  }
  [1]=>
  array(1) {
    ["score_predict_away"]=>
    string(1) "3"
  }
  [2]=>
  array(1) {
    ["score_predict_home"]=>
    string(1) "1"
  }
  [3]=>
  array(1) {
    ["score_predict_away"]=>
    string(1) "3"
  }
如果要

保存整个数组,请使用以下命令:

recording $saveArray = serialize ($myArray);

获取使用数组:

$myArray = unserialize($arrayInMysql);

如果您保存循环内的内容,请将过去的参数放在 foreach 中:

$stmt->bind_param("ss", $ value, $ a3)......

如果您在数据库中保存序列化的应用程序代码,则可能会向后考虑系统。数据库应对您的需求进行建模,应用程序应仅与其交互。

因此,对于足球赛程,您可能希望在数据库中显示多个表,例如:

团队

  • 编号
  • 名字
  • stadium_id

体育场

  • 编号
  • 名字

火柴

  • 编号
  • 日期
  • stadium_id
  • home_team_id
  • home_team_score
  • away_team_id
  • away_team_score

预测

  • 编号
  • match_id
  • user_id
  • home_team_score
  • away_team_score

据推测,这些是网站用户预测的匹配结果,因此user_id列。


如果我正确理解了这个问题,您正在生成一个匹配列表,并允许 peoople 为它们设置分数预测。

您的 HTML 表单现在将如下所示:

<form action="send1.php" method="post" style="font-family: sans-serif;">                 
<?php foreach($matches as $match): ?>
	<div class="fixture">
		<h2>Fixture</h2>
		<div class="fx date"><span>Date:</span> <?= $match['date']; ?></div>
		<div class="fx stadium"><span>Stadium:</span> <?= $match['stadium_name']; ?></div>
		<div class="fx home_team">
			<div class="title">Home Team : <?= $match['home_team_name']; ?></div>
			<div class="score"><label>Predicted score: 
				<input type="text" name="match_prediction[<?= $match['id']; ?>][home_score]" />
			</label></div>
		</div>
		<div class="fx away_team">
			<div class="title">Away Team : <?= $match['away_team_name']; ?></div>
			<div class="score"><label>Predicted score:
				<input type="text" name="match_prediction['<?= $match['id']; ?>][away_score]" />
			</label></div>
		</div>
	</div>
<?php endforeach; ?>
</form>

这应该为您提供一个赛程列表,用户可以在其中插入他们的分数预测。

数据作为数组

数组传递到 PHP,使用匹配 id 作为键。

因此,对于匹配项 1、12 和 34,预测列表可能如下所示:

$_POST = array(
    match_prediction[1]['home_score'] = 1,
    match_prediction[1]['away_score'] = 2,
    match_prediction[12]['home_score'] = 3,
    match_prediction[12]['away_score'] = 1,
    match_prediction[34]['home_score'] = 2,
    match_prediction[34]['away_score'] = 2
);

然后获取该数据以将其插入数据库,您只需遍历 $_POST 数组:

$acMatchPredictions = !empty($_POST['match_prediction']) && is_array($_POST['match_prediction']) ? 
    $_POST['match_prediction'] :
    array();
foreach($acMatchPredictions as $iMatchId => $aPredictedScores) {
    $iHomeScore = (int) $aPredictedScores['home_score'];
    $iAwayScore = (int) $aPredictedScores['away_score'];
    $iUserId = {wherever your user id comes from, session?};
    //query to insert this prediction
    $sqy = "INSERT INTO prediction (match_id, user_id, home_team_score, away_team_score) "
        . "VALUES(?, ?, ?, ?)";
    //prepare the statement
    $stmt = $mysqli->prepare($qry);
    //bind parameters
    if(!$stmt->bind_param('iiii', $iMatchId, $iUserId, $iHomeScore, $iAwayScore)) {
        //trap the error ...
    }
    //execure and so on ...
    // ...
}

这遵循数据库规范化的基本规则:https://en.wikipedia.org/wiki/Database_normalization

如果开始将序列化的应用程序代码放入数据库中,则在更新应用程序时,数据库本身可能会完全过时。如果您将数据与流程完全分离(MVC 背后的原则之一),您将不会遇到此问题。