我试图在我的数据库中插入多个值,这些值来自jQuery脚本,当您单击空格时会生成标记。
我试图在标签上添加一个名称,但它只插入数据库,最后一个标签,我想插入所有标签。
这是我的代码
<script type='text/javascript'>
$(window).load(function(){
function closer(){
$('#close').live('click', function() {
$(this).parent().remove();
});
}
$('#tags').keypress(function(e) {
if(e.which == 32) {
var tx = $('#tags').val();
if (tx) {
$(this).val('').parent().before('<li class="tags"><span><input type="hidden" value="'+tx+'" name="tags">'+tx+'</input></span><a style="cursor:pointer;" id="close">[x]</a></li>');
closer();
}
}
});
});
</script>
<?php
include(PATH_MODULE . '/joboffers/view/joboffers_view.php');
//add job offer function
function addjo(){
connect();
addjoview();
//check if the button was clicked
if (isset($_POST['add_jo'])){
if ($_POST['job_category']) {
$job_category = mysql_real_escape_string($_POST['job_category']);
$check1 = 0;
} else {
$check1 = 1;
}
if ($_POST['description']) {
$description = mysql_real_escape_string($_POST['description']);
$check2 = 0;
} else {
$check2 = 1;
}
if ($_POST['job_name']) {
$job_name = mysql_real_escape_string($_POST['job_name']);
$check3 = 0;
} else {
$check3 = 1;
}
if ($_POST['hiring_manager']) {
$hiring_manager = mysql_real_escape_string($_POST['hiring_manager']);
$check4 = 0;
} else {
$check4 = 1;
}
if ($_POST['tags']) {
$tags = mysql_real_escape_string($_POST['tags']);
$check5 = 0;
} else {
$check5 = 1;
}
if ($_POST['localization']) {
$localization = mysql_real_escape_string($_POST['localization']);
$check6 = 0;
} else {
$check6 = 1;
}
if ($_POST['attributes']) {
$attributes = mysql_real_escape_string($_POST['attributes']);
$check7 = 0;
} else {
$check7 = 1;
}
if ($_POST['contract_type']) {
$contract_type = mysql_real_escape_string($_POST['contract_type']);
$check8 = 0;
} else {
$check8 = 1;
}
if ($_POST['professional_exp']) {
$professional_exp = mysql_real_escape_string($_POST['professional_exp']);
$check9 = 0;
} else {
$check9 = 1;
}
//verify if all fields are right
if ($check1 == 0 && $check2 == 0 && $check3 == 0 && $check4 == 0 && $check5 == 0 && $check6 == 0 && $check7 == 0 && $check8 == 0 && $check9 == 0){
$query =("INSERT INTO job_offers (job_category, description, job_name, hiring_manager, tags, localization, attributes, contract_type, profissional_experience) VALUES ('" . $job_category . "','" . $description . "','" . $job_name . "','" . $hiring_manager . "','" . $tags . "', '" . $localization . "', '" . $attributes . "', '" . $contract_type . "', '" . $professional_exp . "')");
db_query($query, '+');
echo "Congratulations! You have been successfully registered in our system.";
} else {
echo "Ops, something went wrong!";
}
}
}
?>
HTML
<form method="POST">
<br>
<label>Job category</label>
<input type="text" name="job_category" maxlength="100" >
<br><br>
<label>Description</label>
<textarea cols="22" rows="3" name="description"></textarea>
<br><br>
<label>Job name</label>
<input type="text" name="job_name" maxlength="130" >
<br><br>
<label>Hiring manager</label>
<input type="text" name="hiring_manager" maxlength="130" >
<br><br>
<label>Tags</label>
<div id="wrapbox">
<div id="box">
<input type="text" id="tags" class="tagstype" maxlength="230">
</div>
</div>
<br><br>
<label>Localization</label>
<input type="text" name="localization" maxlength="130" >
<br><br>
<label>Attributes</label>
<input type="text" name="attributes" maxlength="130" >
<br><br>
<label>Contract type</label>
<input type="text" name="contract type" maxlength="130" >
<br><br>
<label>Professional experience</label>
<input type="text" name="professional_exp" maxlength="130" >
<br><br>
<input name="add_jo" type="submit" />
</form>
答案 0 :(得分:0)
尝试更改此
<input type="hidden" value="'+tx+'" name="tags">
到这个
<input type="hidden" value="'+tx+'" name="tags[]">
然后,在您的PHP代码中,执行类似这样的操作
if ($_POST['tags']) {
$tags = mysql_real_escape_string(implode(",", $_POST['tags']));
$check5 = 0;
} else {
$check5 = 1;
}
这会将以逗号分隔的代码列表插入到表格的tags
字段中。如果您希望以空格分隔,只需使用implode(" ", $_POST['tags'])
代替。
话虽这么说,我建议您重建数据库,以便在tags
表中没有job_offers
列。相反,您会有一个job_offer_tags
表,其中包含id
,job_id
和tag
列,您只需在tag
列中输入单个标记。