0

I have two forms user_details.php and proposal_details.php. I have two tables: users and proposal_details for respective forms.

users table:

user_id: INT PRIMARY KEY AUTO_INCREMENT

name: varchar(50)

website: varchar(50)...etc..

proposal_details table:

proposal_id: INT PRIMARY KEY AUTO_INCREMENT

user_id: INT FOREIGN KEY...REFERENCE FROM user_details(user_id)...etc

I have stored user_id in a session variable in following way:

user_details.php

session_start();
$run = "SELECT user_id FROM Users WHERE user_id = '$_POST[user_id]'";
$result1 = mysqli_query($con,$run);
$row1= mysqli_fetch_array($result1);
$_SESSION['use_id']= $row1['user_id'];
header("location:proposal_details.php");
exit;

I am trying to insert the data into proposal_details table in following way:

proposal_details.php

<?php
$proposal= $_POST['proposal'];
$proposal_type= $_POST['proposal_type'];
$proposal_template= $_POST['proposal_template'];
$deadline= $_POST['deadline'];
$currency= $_POST['currency'];
$client_name= $_POST['client_name'];
$client_email= $_POST['client_email'];
$comp_name= $_POST['comp_name'];
$website= $_POST['website'];
$txt= $_POST['txt'];
$Country= $_POST['Country'];
$state= $_POST['state'];
$city= $_POST['city'];
$zip= $_POST['zip'];

/*echo $proposal."<br>";
echo $proposal_type."<br>";
echo $proposal_template."<br>";
echo $deadline."<br>";
echo $currency."<br>";
echo $client_name."<br>";
echo $client_email."<br>";
echo $comp_name."<br>";
echo $website."<br>";
echo $txt."<br>";
echo $Country."<br>";
echo $state."<br>";
echo $city."<br>";
echo $zip."<br>";*/



if(!isset($_SESSION)){
    session_start();
}

$user_id = $_SESSION['use_id'];
    $con=mysqli_connect("localhost","root","","my_db"); 
    $sql = "INSERT INTO `proposal_details`(`user_id`, `proposal_name`, `proposal_type`, `proposal_template`, `deadline`, `currency`, `client_name`, `email`, `client_company`, `file_upload`, `website`, `address`, `country`, `state`, `city`, `zipcode`) VALUES

(`$user_id`,`$proposal`,`$proposal_type`,`$proposal_template`,`$deadline`,`$currency`,`$client_name`,`$client_email`,`$comp_name`,`$filePath`,`$website`,`$txt`,`$Country`,`$state`,`$city`,`$zip`)";
    mysqli_query($con,$sql);


}


/* if(isset($_POST['submit1']))
{
validatedata();
insert_data();
//header("location:sections.php");
//exit;
} */
?>


 <h1>WELCOME <?php if(!isset($_SESSION)){session_start();} echo $_SESSION['username']."!"; ?> </h1> 

when I am echoing the values, the values are printed. But the INSERT query is not getting executed in proposal_details.php

Any ideas what I am missing..because I am stuck for 1 hour now.

4
  • mysqli_query($con,$sql) or die(mysqli_error($con)); Commented Jan 27, 2014 at 13:53
  • @user007 Cannot add or update a child row: a foreign key constraint fails (my_db.proposal_details, CONSTRAINT proposal_details_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (user_id)) Commented Jan 27, 2014 at 13:59
  • This means your field user_id is a foreign key (linked) to the users table and you cannot insert values into proposal_details.user_id unless those values are present in users.user_id . Are you getting it? Commented Jan 27, 2014 at 14:04
  • yes.. user_id is getting generated! Commented Jan 27, 2014 at 14:08

1 Answer 1

2

Change this

$sql = "INSERT INTO `proposal_details`(`user_id`, `proposal_name`, `proposal_type`, `proposal_template`, `deadline`, `currency`, `client_name`, `email`, `client_company`, `file_upload`, `website`, `address`, `country`, `state`, `city`, `zipcode`) VALUES

(`$user_id`,`$proposal`,`$proposal_type`,`$proposal_template`,`$deadline`,`$currency`,`$client_name`,`$client_email`,`$comp_name`,`$filePath`,`$website`,`$txt`,`$Country`,`$state`,`$city`,`$zip`)";

to

$sql = "INSERT INTO `proposal_details`(`user_id`, `proposal_name`, `proposal_type`, `proposal_template`, `deadline`, `currency`, `client_name`, `email`, `client_company`, `file_upload`, `website`, `address`, `country`, `state`, `city`, `zipcode`) VALUES

('$user_id','$proposal','$proposal_type','$proposal_template','$deadline','$currency','$client_name','$client_email','$comp_name','$filePath','$website','$txt','$Country','$state','$city','$zip')";

You have `` for values should be ''

Sign up to request clarification or add additional context in comments.

8 Comments

it doesnt change anything.!
probably you have single quote in some values which is breaking the query insert. use this if ( ($result = $mysqli->query($con,$sql))===false ) { echo "Error in query ".$mysqli->error exit(); } This will tell u the error
Cannot add or update a child row: a foreign key constraint fails (my_db.proposal_details, CONSTRAINT proposal_details_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (user_id))
can u echo the $_SESSION['use_id'] in the proposal_details.php and see what it gives ?
yes thats because of this line if(!isset($_SESSION)){ session_start(); } , put session_start(); at the top of the page and then try
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.