gpt4 book ai didi

php - 在 If 语句插入数据库

转载 作者:行者123 更新时间:2023-11-29 00:39:00 24 4
gpt4 key购买 nike

我想做一个 if 语句,在用户点击提交时将特定内容插入 MySQL 数据库。但由于某种原因它不起作用我认为我下面的 if 语句是错误的但我不确定如何表达它。 if($_POST and $_POST['action'] == 'submit'){
我应该在此处正确放置什么才能使它在用户按下提交时将其插入到数据库中?

if($_POST and $_POST['action'] == 'submit'){ 
foreach($_POST as $k=>$v){
$$k = $v;
}
foreach($cat as $k=>$v){
if($v =='') continue;
dbConnect("INSERT INTO twit_info(cat_id) values('". $v ."')");
}
}

下面是我的代码的提交部分。

<form id="add_tweet_form" action="<?=$u?>admin/submit.php" enctype="multipart/form-data" method="post">
<input type="hidden" value="<?=$user_info->id_str?>" name="twitid">
</form>
<form id="go_back_to_user" action="submit" method="post">
<input type="hidden" value="masud" name="username">
<input type="hidden" value="from_tweet" name="submit_username">
</form>
<nav>
<a class="btn float-right" onclick="document.forms['add_tweet_form'].submit();this.innerHTML='Submitting...';" href="javascript:;">Submit</a>
<a class="float-right nav-text gray" title="Don't submit tweet, and go back" onclick="window.location.href='<?=$u?>admin/submit.php';" href="javascript:;">Cancel</a>

最佳答案

可以做这条线:

if($_POST and $_POST['action'] == 'submit'){ 

像这样:

if(isset($_POST['action']) && $_POST['action'] == 'submit'){ 

表格:

<form id="add_tweet_form" action="<?=$u?>admin/submit.php" enctype="multipart/form-data" method="post">
<input type="hidden" value="<?=$user_info->id_str?>" name="twitid"/>
<input type="hidden" value="submitted" name="PostAction" />
</form>
<form id="go_back_to_user" action="submit" method="post">
<input type="hidden" value="masud" name="username">
<input type="hidden" value="from_tweet" name="submit_username">
</form>
<nav>
<a class="btn float-right" onclick="document.forms['add_tweet_form'].submit();this.innerHTML='Submitting...';" href="javascript:;">Submit</a>
<a class="float-right nav-text gray" title="Don't submit tweet, and go back" onclick="window.location.href='<?=$u?>admin/submit.php';" href="javascript:;">Cancel</a>

处理:

if($_POST['PostAction']) && $_POST['PostAction'] == 'submitted'){ 
echo '<pre>';
print_r($_POST); // to see if the right info is posted...
echo '</pre>';
foreach($_POST as $k=>$v){
$$k = $v;
}
foreach($cat as $k=>$v){
if($v =='') continue;
dbConnect("INSERT INTO twit_info(cat_id) values('". $v ."')");
echo 'We have inserted something <br />';
}
}

关于php - 在 If 语句插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13113601/

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