So my problem is that I am unable to insert the data into the selected table using the radio buttons. The radio buttons are used to tell us which table we need to access and to add the data. I thought that I had it correct but it did not work. Here is what I have. Any kind of help would be greatly appreciated.
<h1 align="center">Event</h1>
<h1 align="center">Storage</h1>
<form action="set_event.php" method="post">
<p>
<p>
<input type="submit" name="submit" value="Set Items">
</p>
<p>
<br>
<label>
<input type="radio" name="storage" value="concourse_stairs_s" id="s1">
Concourse Stairs</label>
<br>
<label>
<input type="radio" name="storage" value="bat_cave_s" id="s2">
Bat Cave</label>
<br>
<label>
<input type="radio" name="storage" value="fireside_s" id="s3">
Fireside Storage</label>
<br>
</p>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require ('mysqli_connect.php'); // Connect to the db.
$errors = array(); // Initialize an error array.
}
if (isset($_REQUEST['bat_cave_s']))
{
$q = "INSERT INTO bat_cave_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
else if(isset($_REQUEST['concourse_stairs_s']))
{
$q = "INTO concourse_stairs_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
else if(isset($_REQUEST['fireside_s']))
{
$q = "INSERT INTO fireside_s (item_name, check_in) VALUES ('test', NOW())";
$r = #mysqli_query ($dbc, $q); // Run the query.
}
NOW())";
Your PHP is checking the wrong thing.
You need to check the value of $_REQUEST['storage'].
if($_REQUEST['storage'] == 'bat_cave_s') {
Related
When $_POST submitted anythings working well, But i got this eror Notice: Undefined index: trackingbymobile in /home/../sot_options.php on line ۱۸
if (isset($_POST['optionsformbtn'])&& ($_POST['optionsform'] == 'optionsform')&& ($_POST['trackingbymobile']!=="")){
$wpdb->query($wpdb->prepare( "UPDATE $sotoptions SET option_value = %s WHERE option_name = %s", $_POST['trackingbymobile'], 'trackingbymobile' ));
}
<form name="optionsform" id="optionsform" action="" method="POST">
<input type="checkbox" name="trackingbymobile" id="trackingbymobile">
<input class="sotadminpanelbtn" type="submit" name="optionsformbtn">
</form>
Hi currently I am doing a shopping cart for my project
I would like to ask how can I import the values in a ForEach to the database.
For example, I have the following data in my view.
#foreach (Cart_Has_Services c in Model)
{
<div class="cart-row">
<div class="cart-items">#c.Cart_Service</div>
<div class="cart-items">#c.Additional_Notes</div>
<div class="cart-items">#c.Unit_Price</div>
<div class="cart-items">
<form asp-controller="Cart" asp-action="UpdateCart" formaction="post">
<input type="number" class="item-quantity-input" value="#c.Quantity" />
<input type="submit" class="btn btn-secondary" value="Update" />
</form>
</div>
<div class="cart-items">
<a asp-controller="Cart"
asp-action="DeleteItem"
asp-route-id="#c.Cart_Id"
onclick="return confirm('Delete Serivce #c.Cart_Service')">
Delete
</a>
</div>
</div>
}
As for now, I want to INSERT data (Cart Service, Additional Notes and Quantity) into my database (Order).
In my controller:
public IActionResult Checkout(Cart_Has_Services cart)
{
List<Cart_Has_Services> carts = DBUtl.GetList<Cart_Has_Services>("SELECT * FROM Cart");
string sql = #"INSERT INTO [Order](Order_Name,Order_Description,Order_Quantity)
VALUES('{0}','{1}',{2})";
int ord = DBUtl.ExecSQL(sql, cart.Cart_Service, cart.Additional_Notes, cart.Quantity);
if (ord == 1)
{
TempData["Message"] = "Perofrmance Successfully Created";
TempData["MsgType"] = "success";
return RedirectToAction("Success");
}
else
{
ViewData["Message"] = DBUtl.DB_Message;
ViewData["MsgType"] = "danger";
return View("ShoppingCart");
}
}
I tried the method that I have inserted but it created without inserting the data.
How can I solve this problem?
Hope can get some guidance.
Thank you
The form in the view only submit Quantity, without Cart_Service and Additional_Notes.
To submit their value, you may set hidden inputs in the form. Also you should set name attribute for the input for model binding.
#foreach (Cart_Has_Services c in Model)
{
<div class="cart-row">
<div class="cart-items">#c.Cart_Service</div>
<div class="cart-items">#c.Additional_Notes</div>
<div class="cart-items">#c.Unit_Price</div>
<div class="cart-items">
<form asp-controller="Cart" asp-action="UpdateCart" formaction="post">
<input type="hidden" name="Cart_Service" value="#c.Cart_Service" />
<input type="hidden" name="Additional_Notes" value="#c.Additional_Notes" />
<input type="number" name="Quantity" class="item-quantity-input" value="#c.Quantity" />
<input type="submit" class="btn btn-secondary" value="Update" />
</form>
</div>
<div class="cart-items">
<a asp-controller="Cart"
asp-action="DeleteItem"
asp-route-id="#c.Cart_Id"
onclick="return confirm('Delete Serivce #c.Cart_Service')">
Delete
</a>
</div>
</div>
}
I want to insert data selected from check boxes into a database as well as text which the user types into a text box. I want both the check box values and the text to go into the same row on my database.
My code for abc.php:
<div class="container">
<h2>Please select the answer below:</h2>
<form>
<div class="checkbox">
<label><input type="checkbox" name="response" value="A">A</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="response" value="B">B</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="response" value="C">C</label>
</div>
<form method="post" name="input" action="insert.php" >
Question ID: <input name="questionid" type="text"/><br/>
<input type="submit" name="Submit" value="insert" />
</form>
my code for insert.php
if(isset($_POST["response"]))
{
$query = "INSERT INTO response (student_id, response, question_id) VALUES (:studentID, :response, :questionid)";
$statement = $conn->prepare($query);
$statement->execute(
array(
':response' => $_POST["response"],
':studentID' => $_SESSION['studentid'],
':questionid' => $_POST["questionid"]
)
);
}
When i press the insert button, nothing happens..... can anyone help?
<form action="uploads.php" method="post" enctype="multipart/form-data" id="upload" class="upload">
<fieldset>
<legend>Upload</legend><br/>
Title: <input type="text" name="name" id="name" class="name" required> <br/><br/>
<textarea name="description" rows="6" cols="35" maxlength="120"></textarea><br/>
<input type="file" id="file" name="file[]" required multiple> <br/>
<input type="submit" id="submit" name="submit" value="Upload">
</fieldset>
<div class="bar">
<span class="bar-fill" id="pb"><span class="bar-fill-text" id="pt"></span></span>
</div>
<div id="uploads" class="uploads">
Uploaded file links will appear here.
</div>
<?php
// configuration
$dbhost = "localhost";
$dbname = "blog";
$dbuser = "root";
$dbpass = "pass";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$name = 'name';
$mime = 'mime';
$data = 'data';
$size = 'size';
$description = 'description';
$created = 'created';
$url = 'url';
// query
$sql = "INSERT INTO videos (name,mime,data,size,description,created,url) VALUES (:name,:mime,:data,:size,:description,:created,:url)";
$q = $conn->prepare($sql);
$q->execute(array(':name'=>$name,
':mime'=>$mime,
':data'=>$data,
':size'=>$size,
':description'=>$description,
':created'=>$created,
':url'=>$url));
?>
I'm not so good with PDO, I can get videos to upload to my db, but I can't take in a name or anything. It just shows: name, description, size is 0 and etc. I've watched a few tutorials, but none of them show how to add it by what the user names it or describes it as, only what they put into the values goes to the database. I've also searched around on here and many other websites, but no luck.
In my form, I have x groups of radio buttons generated:
for ( $j = 0; $j < $x; $j++ ) {
<input type="radio" name="gender<?php echo $x; ?>" value="male" />Male
<input type="radio" name="gender<?php echo $x; ?>" value="female" />Female
...
}
When, after submit, I try to retrieve these values:
for ( $j = 0; $j < $x; $j++ ) {
echo $_POST['gender'.$j];
...
}
I get these error messages:
Notice: Undefined index: gender0 in ...
Notice: Undefined index: gender1 in ...
Notice: Undefined index: gender2 in ...
Yet I see these radio groups in my markup correctly defined:
<input type="radio" value="male" name="gender0">Male
<input type="radio" value="female" name="gender0">Female
...
<input type="radio" value="male" name="gender1">Male
<input type="radio" value="female" name="gender1">Female
So why these error messages?
Just make a var_dump($_POST); on the beginning of your receiving script, then you’ll see exactly what you get.
And you are aware that you get values only for radio buttons that are actually checked, right?
So if you can not be certain if the user will have a radio button checked per group, then check whether that POST entry is available before accessing it, using isset or empty.
Btw., an even more convenient way to deal with data like this, is to name the form fields in a way that gets you an array in PHP in the first place:
<input type="radio" name="gender[0]" value="male" />Male
<input type="radio" name="gender[0]" value="female" />Female
<input type="radio" name="gender[1]" value="male" />Male
<input type="radio" name="gender[1]" value="female" />Female
– that will get you an array in $_POST['gender'], that you can loop through with a foreach loop like this:
if(isset($_POST['gender']) && is_array($_POST['gender'])) {
foreach($_POST['gender'] as $index => $value) {
// …
}
}