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);
}
Related
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>
So I have a database with 2 tables and a page with 2 select lists.
According to the value selected on the first select, I want to show the corresponding items on the second select.
There are no errors or anything, but the second select does never show anything, so I'm probably missing something.
Can someone help me?
Connection.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dberror1 = "Could not connect to the database";
$dberror2 = "Could not find the table";
$Conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$Select_db = mysqli_select_db($Conn, 'swimming') or die ($dberror2);
?>
Results.php
<?php
include ("Connections/connection.php");
$query_comp = mysqli_query($Conn, "Select * FROM comp");
?>
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>Swimming Results</title>
<link href="file:///D|/Websites/Swimming/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="wrapper">
<!-- Div for Title -->
<div id="title">
<p>Swimming Results</p>
</div>
<form name="results" >
<!-- Div to Choose competition -->
<div id="competition">
<p>Competition</p>
<select name="sel_comp">
<option value="">---Select one competition---</option>
<?php
while ($fetch = mysqli_fetch_assoc($query_comp)){
echo "<option value='{".$fetch['id']."}'>".$fetch['comp_name']."</option>";
}
?>
</select>
</div>
<!-- Div to Chose event -->
<div id="event">
<p>Event</p>
<select name="sel_even">
<option value="">---Select one event---</option>
<?php
$chosen=$_POST["sel_comp"] ;
$query_even = mysqli_query($Conn, "Select comp.id, events.cid, events.event_name FROM events, comp WHERE events.cid = $chosen");
while ($fetch = mysqli_fetch_assoc($query_even)){
echo "<option value='{".$fetch['event_id']."}'>".$fetch['event_name']."</option>";
}
?>
</select>
</div>
<!-- Div to Show results -->
<div id="results">
<p>Resultados</p>
</div>
</form>
</div>
</body>
</html>
Your PHP code is interpreted server-side, before displaying to the user's browser. So this code can't know anything about the choices the user will do.
If you want a "dynamic sub-select", you need to add some javascript code. That javascript code will run on the user's browser and will be able to react to the user's action on the first select box.
I'm trying to retrieve data from my database using and HTML form and php connection to my sql-workbench. I have a successful connection to the data base and my retrieval is posted at the top of my webpage. I'm not sure how to get it to run the sql query on the submit form function.
Any help would be appreciated, thanks.
<!DOCTYPE html>
<html>
<head data-gwd-animation-mode="quickMode">
<title>Test_webpage</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="generator" content="Google Web Designer 1.1.3.1119">
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "006";
$dbname = "mydb";
// Create connection
$con = new mysqli($servername, $username, $password, $dbname);
//$con->close();
//$query = "SELECT * FROM US_Cars";
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT Manufacturer,VIN,Color,Model,Fuel_Type,State_of_Origin FROM mydb.US_Cars;";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Manufacturer: " . $row["Manufacturer"]. " VIN: " . $row["VIN"]. " Color:" . $row["Color"]. " Model:" . $row["Model"]. " Fuel Type:" . $row["Fuel_Type"]. " State Location:" . $row["State_of_Origin"]. "<br>";
}
} else {
echo "0 results";
}
?>
<div id="first">
<p>Enter Criteria Below to Search.</p>
</div>
<br>
<div id="second">
<p class="gwd-p-j2b6">Select a Manufacturer</p>
<!-- start of form -->
<form class="gwd-form-yuwo" method="post" action="<?search2.php ?>">
<select class="auto-style3 gwd-select-u9g0">
<option value="GM">GM</option>
<option value="Ford">Ford</option>
<option value="Chrysler">Chrysler</option>
<option value="Tesla">Tesla</option>
</select>appreaciated
<br>
<br>
<p class="gwd-p-cy97">Select a Dealership Location</p>
<select class="auto-style3 gwd-select-dkho">
<option value="">Virginia</option>
<option value="">Maryland</option>
<option value="">New Jesery</option>
<option value="">Kentucky</option>
</select>
<br>
<input type="submit" value="Submit" class="auto-style2">
</form>
</div>
<div id="third">
<img id="pic1" src="page2_1.jpg" style="width:500px;" height="236px" >
</div>
<div id="forth">
<img src="tesla.png" style="width:125px;height:250px">
</div>
<div id="fifth">
<footer>
<p>
U.S. Car data Entry
</p>
<p>
About Page
</p>
</footer>
</div>
</body>
</html>
<form class="gwd-form-yuwo" method="post" action="<?search2.php ?>">
is your issue.. pretty sure that is supposed to be
<form class="gwd-form-yuwo" method="post" action="search2.php">
which would send you to search2.php when the submit button was pressed... then in search2.php you would get your form values from $_POST and then run your query.
That being said I think that is not what you are trying to do and what you really want to do is update a list of things within the webpage without linking through. i.e. on button press update html at top of page to list query results using prams from form. If that is what you are trying to do you are way off and need to look up ajax... your submit button would not have a direct action but instead run a javascript fuction that would send an ajax request to search2.php that would run the query and then your java script would update innerhtml on response.
When the page loads i get the undefined index error message enumerated 7 times, I assume it's 1 message per variable.
When I click submit all the form data still get submitted to the DB.
Once I submit the form the Undefined Index error goes away! on page reload.
Weird
<!DOCTYPE html>
<?php
$con=mysqli_connect("localhost","root","","project1");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// check variables set
if (isset($_POST['submit']))
{
$site_code = $_POST['site_code'];
$site_name = $_POST['site_name'];
$site_address = $_POST['site_address'];
$site_city = $_POST['site_city'];
$site_postalcode = $_POST['site_postalcode'];
$province = $_POST['province'];
$country = $_POST['country'];
}
// Query from Countries table
$query_countries = "select * from countries";
$country_results = mysqli_query($con,$query_countries);
$number_of_returns_country = mysqli_num_rows($country_results);
// Query from Provinces Table
$query_provinces = "select * from provinces";
$provinces_results = mysqli_query($con,$query_provinces);
$number_of_returns_province = mysqli_num_rows($provinces_results);
//insert form values into sites table
$sql_site="INSERT INTO sites (site_code, site_name, site_address, site_city, site_postalcode, id_province, id_country)
VALUES
('$_POST[site_code]','$_POST[site_name]','$_POST[site_address]','$_POST[site_city]','$_POST[site_postalcode]',$_POST[province],$_POST[country])";
mysqli_query($con,$sql_site);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<h1>Insert Site into DB</h1>
<h2 class="button"><a href=/index.html>Home</a></h2>
<h2 class="button"><a href=/insert.php>add site</a></h2>
<h2 class="button"><a href=/delete.html>delete site</a></h2>
<h2 class="button"><a href=/search.html>search site</a></h2>
<form class="insert" action="insert.php" method="post">
<h3>Site Info</h3>
Site code: <input type="text" name="site_code"><br>
Site name: <input type="text" name="site_name"><br>
Address: <input type="text" name="site_address"><br>
City: <input type="text" name="site_city"><br>
Postal code: <input type="text" name="site_postalcode"><br>
Province: <select name="province">
<?php while($row = mysqli_fetch_assoc($provinces_results)){ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['province'];?></option>
<?php } ?>
</select><br>
Country: <select name="country">
<?php while($row = mysqli_fetch_assoc($country_results)){ ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['country'];?></option>
<?php } ?>
</select><br>
<h3>Site Contact Info</h3>
Site contact name: <input type="text" name="site_contact_name"><br>
Phone number 1: <input type="number" name="site_contact_number1"><br>
Phone number 2: <input type="number" name="site_contact_number2"><br>
Email address: <input type="email" name="site_contact_email"><br>
<input type="submit">
</form>
</body>
</html>
This happens because, when you load your page for the first time, data of the form isn't set. When you compile and send it, the error doesn't show up simply because now data is set.
You are getting the error, because there is no data in your $_POST variable. To fix it, you will have to add a name to your submit button:
<input type="submit" name="form_posted">
and enclose your PHP code into this if:
if(isset($_POST['form_posted'])) {
}
Alternatively, you can add this on top of your PHP, to exclude warnings:
error_reporting(E_ALL ^ E_WARNING);
so I'm new to php and I have two buttons on this html page here (the id value is included in the url):
<!DOCTYPE html>
<head>
<title>StoryBlox is a Social Story Builder Tool</title>
<meta charset="utf-8">
<!-- these support the header/footer formatting -->
<link type="text/css" rel="stylesheet" href="css/main.css">
<link type="text/css" rel="stylesheet" href="css/header_footer.css">
<script src="js/header.js"></script>
<?php //include_once 'confirm_login.php'
include_once 'story_manager.php';
include_once 'open_connection.php';
//include_once 'functions.php';
//sec_session_start();
if(isset($_GET['id'])){
$str_id = $_GET['id'];
$draft_id = get_story_attribute($str_id, 'draft');
}else{
echo "Invalid story id.";
echo "<br>";
}
?>
</head>
<body>
<div id="wrapper_main">
<div id="wrapper_content">
<?php include_once 'header.php'; ?>
<h1>Welcome to StoryBlox Create Story!</h1>
</div>
<!-- menu -->
<!--<div id="inputs"> -->
<form id="create_form" action="save_story.php?id=<?php echo $str_id?>" method="POST">
<input type="text" name="storyTitle" id="title" placeholder="Enter title." autofocus/><br>
<textarea rows="4" cols="50" name="storyDesc" id="description" placeholder="Enter description here."></textarea>
<div id="footer">
<input type="button" name="draftBtn" onclick="this.form.submit()" value="Save as Draft"/>
<input type="button" name="finalBtn" onclick="this.form.submit()" value="Finished!"/>
</div>
</form>
</div>
</div>
<?php include_once 'footer.php'; ?>
</body>
When I click one of these two buttons, I'm brought to this php document here:
include_once 'open_connection.php';
include_once 'story_manager.php';
$mysqli = open_connection();
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['draftBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_GET['id'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
header('Location: createStory.php');
}
elseif(isset($_POST['finalBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_POST['storyID'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
save_draft_as_completed($str_id);
header('Location: ../home.php');
}else{ echo "failed";}
}?>
And I always get "failed" printed out on my page. I've been Googling this for hours and I don't understand where I'm going wrong here. If anybody could help that would be appreciated. Also, if anyone could shed some light on what the equivalent to
<input type="textarea">
would be that would be great. Thanks!
Use
<input type="submit" name="draftBtn" value="Save as Draft"/>
instead of the button types with their onclick events.
try to use submit tag instead of button.
and if you want to use button tag then you can pass value through hidden field. set value of hidden field on click event.