我想创建一个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>
答案 0 :(得分:1)
可能会这一行:
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 />';
}
}
答案 1 :(得分:0)
添加<input type="hidden" name="add_tweet_form" value="true">
然后检查if(isset($_POST['add_tweet_form']) && $_POST['add_tweet_form']=='true')