Juju Juju - 5 months ago 9
SQL Question

insert values into a specific table chosen from the form

I have a form that works perfectly. I have a table for each categorie in my database. i would like to send values straight into a categorie table that i will choose from the categorie input in the form i tried doing this but it did not work! Any ideas?? thanx alot

$categorie=$_POST['categorie'];
$sql = "INSERT INTO '$categorie' (title, price, description, urlimage) VALUES ('$ad_title', '$ad_price', '$ad_description', '$ad_photo') ";
$conn->query($sql);


<form method="post" role="form" action="" enctype="multipart/form-data">
<div class="row">
<div class="col-lg-12 padding-top-10">
<label for="title" class="control-label">Titre</label>
<input type="text" class="form-control" name="title" placeholder="Title" required></input>
</div>
</div>
<div class="row">
<div class="col-lg-12 padding-top-10">
<label for="price" class="control-label">Prix</label>
<input type="text" class="form-control" name="price" placeholder="Price" required></input>
</div>
</div>
<div class="row">
<div class="col-lg-12 padding-top-10">
<label for="categorie" class="control-label">categorie</label>
<input type="text" class="form-control" name="categorie" placeholder="categorie" required></input>
</div>
</div>
<div class="row">
<div class="col-lg-12 padding-top-10">
<label for="description" class="control-label">Description*</label>
<textarea class="form-control" rows="5" name="description"></textarea>
</div>
</div>
<div class="row">
<div class="col-lg-12 padding-top-10">
<label for="photo" class="control-label">Photo</label>
<input type="file" class="form-control" name="photo" required></input>
</div>
</div>

<div class="row">
<div class="col-md-12 padding-top-10">
<button type="submit" class="btn btn-primary btn-lg btn-block" name="submit">Post</button>
</div>
</div>
</form>

Answer

You should use " (double-quote) or ` back-ticks here.

$sql = "INSERT INTO `$categorie` ...