0
$id = $_SESSION['id'];

i have the table name stored in the $id variable.

when i use the variable name with sql query it doesn't work

$sql = "INSERT INTO karthick.$id (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";

when i replace the karthick.$id as karthick.ford it works fine. but i want to use the variable stored in $id as my table name. how do i do it.

Edit--------------------------- my php code

<?php
session_start();
$id = $_SESSION['id'];
require 'database.php';
/*$sql = "CREATE TABLE karthick.details (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL)";
if($conn->query($sql)===TRUE){
    echo "table created";
}else{
    echo "table not created";
}*/

$name = mysqli_real_escape_string($conn, $_POST["cname"]);
$tin = mysqli_real_escape_string($conn, $_POST["tin"]);
$address = mysqli_real_escape_string($conn, $_POST["address"]);
$product = mysqli_real_escape_string($conn, $_POST["product"]);
$ddate = mysqli_real_escape_string($conn, $_POST["date"]);
$invoice = mysqli_real_escape_string($conn, $_POST["invoice"]);
$transport = mysqli_real_escape_string($conn, $_POST["transport"]);
$cutting = mysqli_real_escape_string($conn, $_POST["date"]);
$amount = mysqli_real_escape_string($conn, $_POST["amount"]);
$vat = mysqli_real_escape_string($conn, $_POST["vat"]);
$val = 'karthick'.$id;
echo $val;
$sql = "INSERT INTO $val (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
if($conn->query($sql)===TRUE){
    echo "record inserted";
}else{
    echo "not inserted".$conn->error;
}
$conn->close();
?>
5
  • "INSERT INTO karthick".$id." Commented Jul 21, 2016 at 10:52
  • $value = 'karthick'.$id; $sql = "INSERT INTO $value " Commented Jul 21, 2016 at 10:52
  • Have you start session in your page?? Commented Jul 21, 2016 at 10:53
  • yes i stared my session @Saty Commented Jul 21, 2016 at 10:59
  • mysqli_select_db($conn,"database_name"); Commented Jul 21, 2016 at 11:14

7 Answers 7

1

Try this one.

$val = 'karthick'.$id;
$sql = "INSERT INTO $val (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";
Sign up to request clarification or add additional context in comments.

1 Comment

it returns no database selected
0

Try using "INSERT INTO karthick.".$id." (name, tin, address, product, invoice, transport

Comments

0

Place the $id in braces{} like karthick.{$id}. The query should be like below.

$sql = "INSERT INTO karthick.{$id} (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";

Comments

0
$val = 'karthick'.$id;
$val = str_replace(" ","",$val);
 $sql = "INSERT INTO $val (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('$name', '$tin', '$address', '$product', '$invoice', '$transport', '$cutting', '$amount', '$vat')";

3 Comments

mysqli_select_db($conn,"database_name");
yes i used mysqli_select_db($conn,"database_name"); it returns the same error
this is the error showing not insertedYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''karthick'.ford (name, tin, address, product, invoice, transport, cutting, a' at line 1
0

Try

$sql = "INSERT INTO karthick.{$id} (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('{$name}', '{$tin}', '{$address}', '{$product}', '{$invoice}', '{$transport}', '{$cutting}', '{$amount}', '{$vat}')";

Comments

0

You are creating karthick.details table instead of karthick.session_id

Comments

0

First method :

Try putting the PHP variables inside curly braces {}

Like:

$sql = "INSERT INTO karthick.{$id} (name, tin, address, product, invoice, transport, cutting, amount, vat) VALUES ('{$name}', '{$tin}', '{$address}', '{$product}', '{$invoice}', '{$transport}', '{$cutting}', '{$amount}', '{$vat}')";

Second method :

Use PHP variables outside quotes

Like:

$sql = " INSERT INTO karthick.".$id." (name, tin, address ..... 

Update

try using `` to enclose your table like,

$val = "`karthick`.`".$id."`";

2 Comments

i tried your first, second and all answers provided here but it returns the same error. not insertedYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' (name, tin, address, product, invoice, transport, cutting, amount, vat) VAL' at line 1
try using `` to enclose your table like, $val = "karthick.".$id."";

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.