gpt4 book ai didi

php - 使用 PHP 将所有 $_POST 数据插入 mysql?

转载 作者:可可西里 更新时间:2023-11-01 07:46:00 25 4
gpt4 key购买 nike

我再次对 STACKOVERFLOW hivemind 提出问题。事情是这样的,我正在尝试将我所有的 $_POST 数据从表单插入到 mysql 表中。之前,我做了:

    INSERT INTO forms (Place, Date, Find, username, who_sponsored, hours, comments, howdiditgo, studentleader_name, studentleader_email, description)
VALUES ('$place', '$date','$where', '$username', '$who', '$hours', '$comments', '$how', '$description',)");

其中所有 $values 都声明为 $_POST['Place']、$_POST['Date'] 等。现在,每次我向表单添加新部分(如另一个文本区域或其他内容)时,我只想在 mysql 中创建一个新列而不是添加另一个 $_POST['foo']。这是我尝试做的事情:

// In the previous form, I have set all input ids to "service[]", so all this data would be in a more specific array than just $POST.  Turns out all the $_POST['service'] stuff is null...  Here's an example: <input name="placeofservice" type="text" id="service[]">


$forms = serialize($_POST['service']);
var_dump($forms);

mysql_query("INSERT INTO forms VALUES('$forms')")
or die(mysql_error());

我一直收到的错误是:列数与第 1 行的值数不匹配。我意识到这意味着我试图将太多数据放入数据库,因为没有足够的列来容纳数据。我已经来回检查过我是否正确(我想我是正确的)。作为引用,这是我的表单和 mysql 表的代码:

<form name="form1" method="post" action="process_form.php">
Place of Service</br>
<input name="placeofservice" type="text" id="service[]"></br>

Date of Service</br>
<input name="dateofservice" type="text" id="service[]"></br>

Where did you find this opportunity?</br>
<input name="where" type="text" id="service[]"></br>

What organization sponsored this opportunity?</br>
<input name="who_sponsored" type="text" id="service[]"></br>

How many hours did you work?</br>
<input name="hours" type="text" id="service[]"></br>

How did it go?</br>
<input type="text" id="service[]" name="howdiditgo" maxlength="100" /></br>

Description of Service:
<textarea name="description" id="service[]" COLS=40 ROWS=6></textarea></br>

Comments:
<textarea name="comments" id="service[]" COLS=40 ROWS=6></textarea></br>

Student Leader Name (If Applicable)</br>
<input name="studentleader_name" type="text" id="service[]"></br>

Student Leader Email(If Applicable)</br>
<input name="studentleader_email" type="text" id="service[]"></br>
<input type="submit" name="Submit" value="Submit">
</form>

Mysql 表:

地点 |日期 |查找 |form_id |谁赞助|小时 |评论 |怎么办 |说明 |学生领袖姓名 | studentleader_email |用户名

注意:我也计划清理我的数据库内容/$POST 数据,但出于我的目的,我将其遗漏了!如果您有任何问题,请随时提出,我将在此处发布并附上编辑:标签 :)

最佳答案

我的功能:

function i($table, $array) {
$query = "INSERT INTO ".$table;
$fis = array();
$vas = array();
foreach($array as $field=>$val) {
$fis[] = "`$field`"; //you must verify keys of array outside of function;
//unknown keys will cause mysql errors;
//there is also sql injection risc;
$vas[] = "'".mysql_real_escape_string($val)."'";
}
$query .= " (".implode(", ", $fis).") VALUES (".implode(", ", $vas).")";
if (mysql_query($query))
return mysql_insert_id();
else return false;
}

关于php - 使用 PHP 将所有 $_POST 数据插入 mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3338138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com