Hello,
I have two tables one for 'users' and another for 'vendor_service' in database named 'star'. In vendor_service table i've stored users id as a foreign key. Now if i try to insert values in vendor_service table it shows following error.
Cannot add or update a child row: a foreign key constraint fails (star/vendor_service
, CONSTRAINT vendor_service_ibfk_1
FOREIGN KEY (id
) REFERENCES users
(id
)).
My Code is here:
<form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Add Service</legend>
<label class="control-label" for="inputWarning">Service Name</label>
<input type="text" id="inputWarning" name="s_name" value="<?php echo $s_name; ?>" />
<label class="control-label" for="inputWarning">Service Category</label>
<select id="selectError" data-rel="chosen" name="s_ctgry" />
<option value="Sound">Sound</option>
<option value="Set&Dom">Set & Dom</option>
<option value="Transport">Transport</option>
</select>
<label class="control-label" for="inputWarning">Service Description</label>
<textarea name="s_desc" rows="4" value="<?php echo $s_desc; ?>" />
</textarea>
<label class="control-label" for="inputWarning">Unit Price</label>
<input type="text" id="inputWarning" name="s_unt_price" value="<?php echo $s_unt_price; ?>" />
<label class="control-label" for="inputWarning">Bulk Price</label>
<input type="text" id="inputWarning" name="s_blk_price" value="<?php echo $s_blk_price; ?>" />
<button type="submit" class="btn btn-primary" name="insert">Insert Service</button>
<button type="reset" class="btn">Cancel</button>
</fieldset>
</form>
<?php
if ( isset( $_POST['insert'] ) )
{
require('include/conn.php'); //include db connection
$s_name = mysql_real_escape_string(htmlspecialchars($_POST['s_name']));
$s_ctgry = htmlspecialchars($_POST['s_ctgry']);
$s_desc = mysql_real_escape_string(htmlspecialchars($_POST['s_desc']));
$s_unt_price = mysql_real_escape_string(htmlspecialchars($_POST['s_unt_price']));
$s_blk_price = mysql_real_escape_string(htmlspecialchars($_POST['s_blk_price']));
$check = "SELECT * FROM vendor_service WHERE s_name='$s_name'";
$run = mysql_query($check);
if ( $s_name == '' ){
echo"Insert Service Name Please</br>";
exit();
}
else if(mysql_num_rows($run) > 0)
{
echo "<script>alert('Service $s_name Already Exist, Please Insert different name')</script>";
}
else if( $s_ctgry == '' ){
echo"Select Category Please</br>";
exit();
}
else if( $s_desc == '' ){
echo"Description Please</br>";
exit();
}
else if( $s_unt_price == '' ){
echo"unit price Please</br>";
exit();
}
else
{
$query = "INSERT into vendor_service (s_name, s_ctgry, s_desc, s_unt_price, s_blk_price)
values('$s_name', '$s_ctgry', '$s_desc', '$s_unt_price', '$s_blk_price')";
if( mysql_query ( $query ) or die(mysql_error()) ){
echo "<script>alert('Service added')</script>"; //put s_prchs_dt varification here
//header("Location: addservices.php");
}
else
{
echo"<script>alert('There's some problem Some problem')</script>";
}
}
}
?>