我试图在数据库表中插入一个值,但无法插入它。在无法为wordpress数据库中的国家和日期选择器的customer_info表插入之后,它一直在注释字段之前插入一个值。那是正确的方法吗?
这是我的html代码:
<div style=" text-align: center;">
<form action="#v_form" method="post" id="v_form">
<label for="visitor_name"></label>
<input class="contedd" type="text" name="visitor_name" id="visitor_name" placeholder="Your Name"/>
<label for="visitor_name"></label>
<input class="contedd" type="text" name="visitor_email" id="visitor_email" placeholder="An email id we can reach you at." />
<label for="Your Number"></label>
<input class="contedd" type="text" name="YourNumber" id="YourNumber" placeholder="Your Number" />
<br>
<label for="comment"></label>
<select class="contedd" name="npofpeople" style=" margin-top: 10px; padding: 6px; width: 100%; font-size: 15px; border-radius: 1px; border: 1px solid #811a53;">
<option value="" disabled selected>How many people you will be ?</option>
<option value="1-5">1-5</option>
<option value="6-10">6-10</option>
<option value="10-15">10-15</option>
<option value="above">above</option>
</select>
<label for="datepicker"></label>
<input class="contedd" type="text" name="datepicker" id="date_ex" placeholder="When do you want to visit us ?">
<label for="comment"></label>
<textarea class="contedd" name="comment" rows="5" cols="40" placeholder="We will arrange for your travel . Please give your full address" style="
margin-top: 10px; padding: 6px; width: 100%;font-size: 15px;border-radius: 1px; border: 1px solid #811a53;"></textarea>
<label for="country"></label>
<select class="contedd" name="country" style=" margin-top: 10px; padding: 6px; width: 100%; font-size: 15px; border-radius: 1px; border: 1px solid #811a53;
">
<option value="" disabled selected>Country</option>
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
</select>
<input type="submit" name="submit_form" value="submit" style="text-align: center; margin: 0 auto; margin: 10px 0px 0px 0px;">
</form>
</div>
<?php
$html = ob_get_clean();
// does the inserting, in case the form is filled and submitted
if ( isset( $_POST["submit_form"] ) && $_POST["visitor_name"] != "" ) {
$table = $wpdb->prefix."my_table";
$name = strip_tags($_POST["visitor_name"], "");
$visitor_email = strip_tags($_POST["visitor_email"], "");
$YourNumber = strip_tags($_POST["YourNumber"], "");
$comment = strip_tags($_POST["comment"], "");
$npofpeople = strip_tags($_POST["npofpeople"], "");
$datepicker = strip_tags($_POST["datepicker"], "");
$country = strip_tags($_POST["country"], "");
$wpdb->insert(
$table,
array(
'name' => $name,
'visitor_email' => $visitor_email,
'YourNumber' => $YourNumber,
'comment' => $comment,
'npofpeople' => $npofpeople,
'datepicker' => $datepicker,
'country ' => $country ,
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
)
);
$html = "<p>Your name <strong>$name</strong> was successfully recorded. Thanks!!</p>";
}
答案 0 :(得分:0)
抱歉! 不,开个玩笑。 请查看WPDB文档,insert方法需要3个参数,而不是2个。像这样从数据数组中取出格式数组:
$wpdb->insert(
$table,
array(
'name' => $name,
'visitor_email' => $visitor_email,
'YourNumber' => $YourNumber,
'comment' => $comment,
'npofpeople' => $npofpeople,
'datepicker' => $datepicker,
'country ' => $country
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
答案 1 :(得分:0)
尝试此代码。删除国家/地区字段'country ' => $country
至'country' => $country
$wpdb->insert(
$table,
array(
'name' => $name,
'visitor_email' => $visitor_email,
'YourNumber' => $YourNumber,
'comment' => $comment,
'npofpeople' => $npofpeople,
'datepicker' => $datepicker,
'country' => $country
),
array( '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);