So I Have The following code, I am trying to post the multiple data in the div (cart) to my sql database,how do I get the right code to post the following to my sql database?
If its not possible for multiple names on the match, how do i get it to insert multiple matches at once?
I am beginner level on php, please help, if you can draft for me even a different but relatable model
most of the answers i find online can only post but this one is a cart so I only need the data from the div to be posted not edited
here is my code
<!DOCTYPE html>
<html>
<head>
<title>Add Records in Database</title>
</head>
<body>
<?php
include "dbConn.php"; // Using database connection file here
if(isset($_POST['submit']))
{
$match = $_POST['match'];
$selection = $_POST['selection'];
$odd = $_POST['odd'];
$totalOdds = $_POST['totalOdds'];
$stakeAmount = $_POST['stakeAmount'];
$payout = $_POST['payout'];
$insert = mysqli_query($db,"INSERT INTO `receipts`(`Match`, `Selection`, `Odd`, `Total Odds`, `Stake Amount`, `Payout`)
VALUES ('$match','$selection','$odd','$totalOdds','$stakeAmount','$payout')");
if(!$insert)
{
echo mysqli_error($db);
}
else
{
echo "Records added successfully.";
}
}
mysqli_close($db); // Close connection
?>
<div class="cart">
<div class="title">Bet Slip</div>
<div id="box" class="boxlit" >
<form action="" method="post">
<div class="box" data-id="4" name="Match">Lanus - Godoy Cruz<div name="selection">Draw</div><div class="crtTotal" name="odd">3.05</div></div>
<div class="box" data-id="8" name="Match">Deportivo Pasto - Envigado<div name="selection">Away</div><div class="crtTotal" name="odd">4.70</div></div>
<div class="box" data-id="9" name="Match">Union Manabita - Emelec<div name="selection">Home</div><div class="crtTotal" name="odd">8.25</div></div>
<div class="box" data-id="12" name="Match">New Jersey Copa - FC Motown II<div name="selection">Home</div><div class="crtTotal" name="odd">3.35</div></div></div>
<br>
<div>Total Odds: <b id="ct1" name="totalOdds" >475.42</b></div><br>
<div>(N$)Stake: <input id="stake" type="number" name="stakeAmount" value="5"> NAD</div><br>
<div>Payout: N$ <b id="payout" name="payOut" >2377.10</b></div>
<input type="submit" name="submit" value="Bet">
</form>
<div class="footer"></div>
</div>
</body>
</html>
Related
I am working on this program that gets some inputs from the user and then uses that input to retrieve data from a few tables in a MySQL database. My issue is that it seems like I am not getting anything from the MySQL tables, and it would be great if you could help.
dbConn.php
<?php
$ser = "localhost";
$user = "root";
$pass = "pass";
$db = "dbvisual";
$conn = mysqli_connect($ser, $user, $pass, $db) or die("connection failed");
echo "connection success";
?>
form.php
<?php
if(isset($_Post['submit'])){ // Checking to see if the form has been submitted
$battery = (int) $_POST['battery']; // Battery Input element
$cycle = (int) $_POST['cycle']; // Cycle input element
$xVar = $_POST['X']; // X variable
$yVar = $_POST['Y']; // Y variable
// Trying to get the x variable based on what the user said
$mySqlQueryX = "SELECT cap
FROM cycle
WHERE cycleID = $cycle";
$feedbackX = mysqli_query($conn, $mySqlQueryX);
echo "<h1>$feedbackX</h1>";
}
?>
index.php
<!-- Connecting to the database -->
<?php
include 'dbConn.php';
?>
<!-- The Styling of the website -->
<style>
<?php include 'main.css'; ?>
</style>
<!-- The Skeleton of the website -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php include "form.php" ?>
<!-- The entire website -->
<div id="body">
<!-- The input half -->
<div id="inputs">
<!-- The input form -->
<form action="index.php" method="POST">
<div id="form">
<!-- Labels -->
<div id="label">
<label for="">Which Batteries?</label>
<label for="">What Cycles?</label>
<label for="">What's X?</label>
<label for="">What's Y?</label>
<label for="">Discharge?</label>
<label for="">Charge?</label>
</div>
<!-- User Info -->
<div id="info">
<input type="text" placeholder="Batteries" required
oninvalid="this.setCustomValidity('No Batteries Were Entered')"
oninput="setCustomValidity('')" name="battery">
<input type="text" placeholder="Cycles" required
oninvalid="this.setCustomValidity('No Cycles Were Entered')"
oninput="setCustomValidity('')" name="cycle">
<select name="X">
<option value="cap">Capacity</option>
<option value="speCap">Specific Capacity</option>
<option value="voltage">Voltage</option>
</select>
<select name="Y">
<option value="cap">Capacity</option>
<option value="speCap">Specific Capacity</option>
<option value="voltage">Voltage</option>
</select>
<input type="checkbox" name="discharge" checked>
<input type="checkbox" name="charge" checked>
</div>
</div>
<!-- Submit Button -->
<div>
<button type="submit" name="submit">Submit</button>
</div>
</form>
</div>
<!-- The graph half -->
<div id="graph">
<p>hello</p>
</div>
</div>
</body>
</html>
When I try to "echo" what I am supposed to get from the database, nothing shows up and I am guessing the issue could be related to mysqli_query() having problems with $conn.
Thank you all!
You need to change your form.php file this is what I propose
<?php
if(isset($_POST['submit'])){
$battery = (int) $_POST['battery']; // Battery Input element
$cycle = (int) $_POST['cycle']; // Cycle input element
$xVar = $_POST['X']; // X variable
$yVar = $_POST['Y'];
$con = mysqli_connect("localhost","root","passer","arduino");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$mySqlQueryX = "SELECT cap
FROM cycle
WHERE cycleID = '".$cycle."' ";
$result = mysqli_query($con, $mySqlQueryX);
// Fetch all
print_r(mysqli_fetch_all($result, MYSQLI_ASSOC));
// Free result set
mysqli_free_result($result);
mysqli_close($con);
}
I have a problem to get the id of a task in order to delete that task when the user clicks on the delete button of that task in the UI. I have two tables, one is the "To-do Task" table, and the other is the "Completed Task" table. In the code, I did ask the user what task in what table they want to delete via form by using the task's id and the table name. Now I do not want to use the form, but I want to have a delete button next to each task in each table so that the user just needs to click on that button to delete the task. Can you teach me how to do it? Thank you
<?php
session_start();
require 'connect.php';
$owner = $_SESSION['name'];
//delete data in the table the user want based on id of the data
if (isset($_POST['delete'])) {
$section = $_POST['delete_com_in'];
$task_id=$_POST['delete_com_in_id'];
$deleteQuery="DELETE FROM $section WHERE id=:task_id";
$preparedDeleteStatement = $conn->prepare($deleteQuery);
$preparedDeleteStatement->bindValue(':task_id',$task_id);
$valueDelete=$preparedDeleteStatement->execute();
}
//fetch data into table (incomplete and complete)
$displayQuery="SELECT * FROM incomplete where owner=:owner";
$displayTask= $conn->prepare($displayQuery);
$displayTask->bindValue(':owner', $owner);
$displayTask->execute();
$allTask=$displayTask->fetchAll();
echo "<table class=\"incomplete_table\"><caption>To-do Tasks</caption><tr><th>ID</th><th>Title</th><th>Description</th><th>Due Date</th><th>Time</th></tr>";
if(count($allTask) > 0)
{
foreach ($allTask as $row) {
echo "<tr><td>".$row["id"]."</td><td>".$row["title"]."</td><td>".$row["description"]."</td><td>".$row["due_date"]."</td><td>".$row["time"]."</td></tr>";
}
}
$displayQueryComplete="SELECT * FROM complete where owner=:owner";
$displayTaskComplete= $conn->prepare($displayQueryComplete);
$displayTaskComplete->bindValue(':owner', $owner);
$displayTaskComplete->execute();
$allTaskComplete= $displayTaskComplete->fetchAll();
echo "<table class=\"complete_table\"><caption>Completed Tasks</caption><tr><th>ID</th><th>Title</th><th>Description</th><th>Due Date</th><th>Time</th></tr>";
if(count($allTaskComplete) > 0)
{
foreach ($allTaskComplete as $row) {
echo "<tr><td>".$row["id"]."</td><td>".$row["title"]."</td><td>".$row["description"]."</td><td>".$row["due_date"]."</td><td>".$row["time"]."</td></tr>";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./main_list.css">
<link href="https://fonts.googleapis.com/css2?family=PT+Sans&display=swap" rel="stylesheet">
<script src="./add_task.js" defer></script>
<title>Main List</title>
</head>
<body>
<div id="container">
<button id="buttonMainList">Logout</button>
<p id="userNameHere"><?php echo $_SESSION['name']; ?></p>
</div>
<h1 id="inform">TO-DO LIST</h1>
<div id="menu">
<label for="action">Choose an action:</label>
<select id="action" name="action" onchange='onSelectChangeHandler()'>
<option value="delete">Delete</option>
</select>
</div>
<!--delete form-->
<div id="delete">
<div id="delete_task_form">
<p id="delete_task">Delete Task</p>
<form id="delete_form" name="delete_form" method="post">
<div id="delete_section">
<input type="radio" id="delete_com_complete" name="delete_com_in" value="complete">
<label for="delete_com_complete">Completed</label>
<input type="radio" id="delete_com_incomplete" name="delete_com_in" value="incomplete">
<label for="delete_com_incomplete">Incomplete</label>
</div>
<div id="delete_section_id">
<label for="delete_com_in_id">What ID?</label>
<input type="text" id="delete_com_in_id" name="delete_com_in_id"><br>
</div>
<input id="submit_delete_form" type="submit" name="delete" value="Delete">
</form>
<br>
</div>
</div>
</div>
</body>
Well, it's not a correct way of work but without a form you can use the tag to pass throught get the id of the task to delete:
<html>
Delete button
</html>
Also, you need to known the table that the user want delete so you need to create two buttons each one will represent one table:
<html>
Delete button table 1
Delete button table 2
</html>
98 is an exemple of a id. When you have passed the id of the task you only have to receive it by $_GET:
<?php
if(isset($_GET["task"])){
$Task=$_GET["task"];
}
?>
Once you have the id you have to do the same with the table:
<?php
if(isset($_GET["table"])){
if($_GET["table"]=="To-do Task"){
$Table="To-do Task";
}else{
$Table="Completed Task";
}
}
?>
Finally you only have to delete the task depending the table:
<?php
if($Table=="To-do Task"){
$deletetask= $con->prepare("DELETE FROM `To-do` Task WHERE ID=?;");
$deletetask->execute([$Task]);
}else{
$deletetask= $con->prepare("DELETE FROM Completed Task WHERE ID=?;");
$deletetask->execute([$Task]);
}
?>
Remember always sanitize and validate inputs from the users or values than can be manipulated by the user
****I'm trying to get submiitted value in database.After getting the value from a particular table i want to store the checked value into another table with the same columns.How to add the values that come from database while submitting after the checked valueHere's my code****
form.php
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<title></title>
</head>
<body>
<form action="insert.php" method="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="text">Sec</label>
<input type="text" class="form-control" name="sec" id="sec">
</div>
<button type="submit" name="submit" class="btn btn-default">Submit</button>
<button type="submit" name="getdata" class="btn btn-default">Get</button>
</form>
</body>
</html>
insert.php
<?php
$con=mysqli_connect("localhost", "root", "","input");
// inserting data
if(isset($_POST['submit'])){
$name=$_POST['name'];
$sec=$_POST['sec'];
if($name !=''||$sec !=''){
$query = mysqli_query($con,"insert input_form(Name,Sec) values ('$name', '$sec')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
else if(isset($_POST['getdata'])){
$query1 = mysqli_query($con,"select * from input_form");
while ($row1 = mysqli_fetch_array($query1)) {
?>
<ul class="form-get">
<input type="checkbox" name="chk[]" ><li><?php echo $row1['Name'];?><?php echo $row1['Sec']; ?> </li>
</ul>
<?php
}
?>
<button name="present">Submit</button>
<?php
}
?>
<?php
if(isset($_POST['present'])){
$checkbox=$_POST['chk'];
for($i=0;$i<sizeof($checkbox);$i++){
$query2=mysqli_query($con,"insert into present(Name,Sec) values ('".$checkbox[i]."')");
}
}
?>
<?php
mysqli_close($con);
?>
you should set a status for checked value to "1" and "0" to unchecked to store into database.Make a condition that if the variable is checked and have status to "1" then it will be submitted to database otherwise not.
here you can make a condition.
First of all you have missing a tag for check box in the form
<label for="checkbox">check</label>
<input type="checkbox" class="form-control" name="chk" id="chk">
here you can post the checkbox status
$chk=$_POST['chk'];
////////////create a column for check box also to make it easy./////
if(isset($_POST['present'])){
$checkbox=$_POST['chk'];
for($i=0;$i<sizeof($checkbox);$i++){
if($checkbox===1){
$query2=mysqli_query($con,"insert into present(Name,Sec,checkbox) values
('".$checkbox[i]."')");
}
}
}
hope it may help you.
Below is my code(jquery mobile and php) I am trying to insert into the database and also echo the following message (pls fill all field and registration complete) that is if the user complete the field or not the following message show display but non of it is working with my jquery mobile code and it is working with my normal site how can I fix this I will appreciate it if you work on my code thank you
<?php
$db= “user”;
$connect = mysql_connect(“localhost“, “alluser”, “six4”)or die(“could not connect”);
Mysql_select_db($db) or die (“could not select database”);
If (isset($_POST['submit'])){
If(empty($_POST['name']) OR empty($_POST['email']) OR empty($_POST['add'])){
$msg = 'pls fill all field';
$name = ($_POST['name']);
$email = ($_POST['email']);
$address = ($_POST['add']);
mysql_query(“INSERT INTO people (Name, Email, Address”) VALUES ('$name, $email, $address')”) or die (mysql_error());
$msg='registration complete ';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="css/jquery.mobile-1.0a1.min.css" />
<script src="js/jquery-1.4.3.min.js"></script>
<script src="js/jquery.mobile-1.0a1.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<h1>User</h1>
</div>
<div data-role="content">
<?php echo “$msg”; ?>
<form name=“form” action=“” method=“post”>
<label for=“name”>Name</label>
<input type=“text” name=“name” />
<label for=“email”>Email</label>
<input type=“text” name=“email” />
<label for=“address”>Address</label>
<input type=“text” name=“add” />
<input type=“submit” name=“submit” value=“Submit” />
</form>
</div>
<div data-role="footer">
<h4>Page Footer</h4>
</div>
</div>
</body>
</html>
Check your brace positions after your if statements. You check for empty values, but you don't alter the program flow in a meaningful way if you find them.
Also, replace your curly quotes with real quotes. And check for SQL injection. And double-check your MySQL call. You'll get an error from PHP before you'll ever get $msg echoed, based on the way things are written.
Was hoping for some fresh eyes on this. I've got a page which will add a new event to a database. All 3 fields are required, so if they are empty when the submit button is pushed the error message should appear. Anyone know why it isn't showing the error message? Thanks in advance!
Here's the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>ADMIN - Repetative Strain Injury UK</title>
<meta http-equiv="content-type" content="application/xhtml; charset=utf-8" />
<link href="/iis_project/view/css/mycss.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<!-- wrapper div for positioning -->
<div class="grid10 first" id="header">
<!-- Header Section -->
<img src="/iis_project/view/img/banner.jpg" alt="RSI UK Banner" width="1090" height="75"/>
</div>
<div class="grid11 first" id="date">
<!-- Date Section -->
<?php
include ("date.php");
?>
</div>
<div class="grid2 first" id="col1">
<!-- Left column Section -->
<h1>Navigation</h1>
<p>
<strong>Home</strong>
</p>
<p>
<strong>About Us</strong>
</p>
<p>
<strong>Register</strong>
</p>
<p>
<strong>Log In</strong>
</p>
<p>
<strong>Events</strong>
</p>
<p>
<strong>Acknowledgements</strong>
</p>
</div>
<div class="grid6" id="col2">
<!-- Middle column Section -->
<h1>New Event</h1>
<p>
As an admin you have the right to create a new event. Simply fill out the form below and hit submit.
</p>
<?php
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($name, $date, $location, $error22)
{
?>
<?php
// if there are any error22s, display them
if ($error22 != '') {
echo '<div style="padding:4px; border:1px solid red; color:red;">' . $error22 . '</div>';
}
?>
<form action="" method="post">
<div>
Name:
<input type="text" name="name" value="<?php echo $name;?>" />
<br/>
Date:
<input type="text" name="date" value="<?php echo $date;?>" />
<br/>
Location:
<input type="text" name="location" value="<?php echo $location;?>" />
<br/>
<input type="submit" name="submit_event" value="Submit">
</div>
</form>
<?php
}
// connect to the database
include ("home_connection.php");
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$location = mysql_real_escape_string(htmlspecialchars($_POST['location']));
// check to make sure all fields are entered
if ($name == '' || $date == '' || $location == '')
{
// generate error22 message
$error22 = 'error22: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($name, $date, $location, $error22);
}
else
{
// save the data to the database
mysql_query("INSERT events SET name='$name', date='$date', location='$location'")
or die(mysql_error22());
// once saved, redirect back to the view page
header("Location: login_success_admin.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','');
}
?>
</div>
<div class="grid2" id="col3">
<!-- Right column Section -->
<h1>Newsletter</h1>
<h2>Sign up to our newsletter:</h2>
<?php
include ("newsletter.php");
?>
</div>
<div class="grid10 first" id="footer">
<!-- Footer Section -->
<p>
Repetative Strain Injury UK © West Street, Jubille Way, Leeds, LS6 3QW. Email: info#rsiuk.org
Tel: 0113 658102. Reg charity no: 1032941
</p>
</div>
</div> <!-- end container -->
</body>
</html>
You have the submit name as "submit_event"
So you must use
if (isset($_POST['submit_event'])) {
.
.
.
}
Hope it helps
Try adding
<input type="hidden" name="submit" value="1" />
to your form. The $_POST array does not have a 'submit' value in the code you have provided, so it's just going to display the form again.