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>";
            }
        }
    }
?>
Member Avatar
diafol

I assume that you're talking about line56. In order to insert a record you need to include an user_id otherwise why did you include a constraint in the first place? Are these tables related or not? If not pull the user_id field. I haven't studied your code too closely, but sometimes we can create what's called a link table. I don't think that is applicable here though as you need a 1 to many relationship. IMO.

Table 'users' is my master table in which 'id' is primary key and another table 'vendor_service' has 'id' as a foreign key. If user is logged in and try to insert values in vendor_service, i want id should be automatically inserted in vendor_service table from the user's id through which currently signed in.

Member Avatar
diafol

That was not your original question or the subject of the title. In fact you didn't pose a question.

So - what's the problem with including a user_id from the session into your insert query?

The problem is that I'm getting following error when i execute above query...

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)).

What is solution to this error ?

Member Avatar
diafol

As I said, you haven't included the user_id in the INSERT query. Get the user_id value from your session variables and include it. You say that you have a login procedure ("If user is logged in...") so I assume that you're storing the user_id in a session.