Getting a singular value from a SQL table - php

I am trying to fetch a value from one of my tables, however it doesn't appear to be retrieving it as when I echo the variable nothing is displayed.
Here is my code
<?php
$conn = new mysqli('localhost','root','root','neadatabase'); //Creating
connection to database
//Connection Error Checking
if ($conn->connect_error)
{
die("Connection Failed: " . $conn->connect_error);
}
echo ("Connection Successful");
//selecting bookings
$sqlSelectBookings = "
SELECT ActivID, DateRequired, TimeRequired
FROM activbooking
WHERE ActivID = '$_POST[ActivID]' AND DateRequired =
'$_POST[DateRequired]' AND '$_POST[DateRequired]' AND 'TimeRequired = '
$_POST[TimeRequired]'
";
$sqlSelectBookings = $conn->que($sqlSelectBookings);
//getting member limit
$sqlGetMemberLimit = "
SELECT memberLimit
FROM activity
WHERE activID = '$_POST[ActivID]'
";
$sqlGetMemberLimit = ($conn->query($sqlGetMemberLimit)-
>fetchObject(memberLimit));
echo $sqlGetMemberLimit;
Any ideas?
This is what happens when I run the page
Thanks

Related

I have problem in insert data into database but didn't show any error

I have problem with inserting my data into database
usually if something wrong it's showing error but this one not showing error (I don't know if this count as error or not cause I'm still new with this php coding stuff)
<?php
/*proses*/
$conn = mysqli_connect('localhost','root', '','dsolo');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$id = $_POST['id'];
$bulan = $_POST['bulan'];
$gajipokok = $_POST['gajipokok'];
$totalbonus = $_POST['totalbonus'];
$potongan = $_POST['potongan'];
$totalgaji = $_POST['totalgaji'];
$pajak = $_POST['pajak'];
$tes="INSERT INTO gaji set id='$id',
bulan='$bulan',
gajipokok='$gajipokok',
totalbonus='$totalbonus',
potongan='$potongan',
totalgaji='$totalgaji',
pajak='$pajak',";
mysqli_query ($conn,$tes) or die ($tes);
?>
After I press save what is showing is this one instead
INSERT INTO gaji set id='yuki', bulan='2019-08', gajipokok='2000000', totalbonus='2000000', potongan='1000000', totalgaji='3000000', pajak='300000',
What I need to change in my code?
There is an extra , in your query, you can remove that and your code look like the below code..
<?php
$conn = mysqli_connect('localhost','root', '','dsolo');
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
$id = $_POST['id'];
$bulan = $_POST['bulan'];
$gajipokok = $_POST['gajipokok'];
$totalbonus = $_POST['totalbonus'];
$potongan = $_POST['potongan'];
$totalgaji = $_POST['totalgaji'];
$pajak = $_POST['pajak'];
$tes="INSERT INTO gaji set id='$id', bulan='$bulan', gajipokok='$gajipokok', totalbonus='$totalbonus',potongan='$potongan', totalgaji='$totalgaji', pajak='$pajak'";
mysqli_query ($conn,$tes) or die ($tes);
}
?>

PHP - Mysqli_query is successful, but return empty rows

I have a table full of names and I have a stored procedure that returns all of the names. However it is not doing what I expect it to do and I can't pinpoint the problem even with log. Instead of getting a drop down list of actual names, I'm getting a drop down of blank values as if I can actually select a value, but console.log displays a empty value, which I'm assuming means it's null
connection.php
<?php
require 'credentials.php';
include '../ChromePhp.php';
$con = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($con->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
ChromePhp::warn('something went wrong!'); // didn't reach here
}
?>
This calls retrieveAllPokemonNames.php
<?php
include '../database/retrieveAllPokemonNames.php';
while ($row = $result->fetch_assoc()){
echo "<option value= ".$row[0].">".$row[0]."</option>";
}
?>
retrieveAllPokemonNames.php
<?php
require 'connection.php';
$sql = "CALL sp_selectAllPokemonName";
if ($result = $con->query($sql)){
ChromePhp::log('query is successful'); // output
ChromePhp::log($result); // log outputs a Object { current_field: null, field_count: null, .....etc }
}
?>
The SQL query works fine when I call it in phpMyAdmin's interface and sp_selectAllPokemonName is basically
Select * From PokemonName
which returns ~100 rows

Moving deleted records to another table

I am trying to move the records from table studentrecords to passivestudents but i am not being able to. so far i've tried.
<?php
$db = new mysqli("localhost", "root", "","learndb");
if ($db->connect_error) {
die("Connection failed this is the error: " . $db->connect_error);
}
$id=$_GET['id'];
$sql="INSERT INTO passivestudents VALUES (DELETE FROM studentrecords WHERE id=$id)";
$result=$db->query($sql);
if(!$result)
{
echo"ERROR MOVING";
}
else
{
echo "<center><p style=\"color:green\">Information Moved!</p></center>";
}
?>
your query was wrong . you want to remove student with some id ? true ?
so follow these step:
1-INSERT INTO your_sec_table (select * from first_Table where id= $id;
2-DELETE FROM first_table WHERE id=$id

How do I redirect with PHP and save the link in SQL?

I have XAMPP and I want to write a simple PHP page, that redirects me to the link that I specify, and also saves the link in an SQL database.
Let's say I want to visit www.google.com:
I'd visit something like:
localhost:80/redirect.php?url=https://google.com
And PHP would redirect me there and also save the www.google.com link in an SQL table.
Can you help me out?
Considering how you formed your question, it looks as if you had an idea an just want someone to give you the solution without you even making an effort (please correct me if I'm wrong but that's how it seams...)
The task you are trying to achieve is a simple one, and it's only fair to point you in the right direction. your "task" can be broken into several smaller ones:
Create database / table for storing data | PHP Create MySQL Tables
Get URL parameter in PHP
PHP Insert Data Into MySQL
How to make a redirect in PHP
Sorry if this is not the kind-a answer you are looking for, but I figure the point of this website is for people to learn something and not just copy+paste. The provided links can be used to solve your task problem.
This is what I came up with, after MySQLi Object-oriented did not validate this:
$sql = "SELECT * FROM logging WHERE link=$link";
if ($conn->query($sql) === TRUE) {}
It still increments the number of visits sometimes by +2. I don't know why.
<?php
$servername = " ";
$username = " ";
$password = " ";
$dbname = " ";
$datetime = date_create()->format('Y-m-d H:i:s');
$datetime = "'".$datetime."'";
$link_clean = $_GET['link'];
$link = "'".$link_clean."'";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM logging WHERE link=$link";
if ($result = mysqli_query($conn, $sql))
{
if(mysqli_num_rows($result)>0)
{
$sql="UPDATE logging SET last_visit_date = $datetime, visit_count = visit_count + 1 WHERE link=$link";
if (mysqli_query($conn, $sql)) {
$conn->close();
header("Location: https://$link_clean");
exit;
} else {
echo "1Error: " . $sql . "<br>" . mysqli_error($conn);
$conn->close();
exit;
}
}
else
{
$sql="INSERT INTO logging (link, last_visit_date, visit_count) VALUES ($link , $datetime , 1)";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
header("Location: https://$link_clean");
exit;
} else {
echo "2Error: " . $sql . "<br>" . mysqli_error($conn);
mysqli_close($conn);
exit;
}
}
}
else
{
echo "3Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

Switched computers and get two new mysqli_fetch_row errors.

after I managed to connect my website form to my database, I decided to try to transfer over my files to my work computer.
Initially I only had one error: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in...
However now I get an extra mysqli_fetch_row() error the same as above but the error is on a different line.
Additionally I also get the error: Undefined index: fill which I never got before. Are there any mistakes in my code? The form still works and can connect to my database.
<center><form action="fill.php" method="post">
Fill
<input type="text" id="fill"" name="fill">
<input type="submit" id ="submit" name="submit" value="Submit here!">
</form></center>
</div>
<?php
$val1 = $_POST['fill'];
$conn = mysqli_connect('localhost', 'root', '')or
die("Could not connect");
mysqli_select_db($conn, 'rfid');
$val2 = "SELECT * FROM card_refill WHERE refill = $val1";
$result1= $conn->query($val2);
$row = mysqli_fetch_row($result1);
$refill1 = $row[2];
$value = "SELECT *FROM card_credit ORDER BY id DESC LIMIT 1:";
$result = $conn->query($value);
$row = mysqli_fetch_row($result);
$refill = $row[2];
$money= $refill+$refill1;
echo $money;
$sql = "UPDATE card_credit SET value = '$money'";
if ($conn->query($sql) === TRUE) {
echo "Success";
}
else {
echo "Warning: " . $sql . "<br>" . $conn->error;
}
mysqli_close($conn);
?>
</body>
</html>
You're getting that error because you use $_POST['fill'] without checking whether it's set first. It will only be set when the form is submitted, not when the form is first displayed. You need to put all the code that processes the form input into:
if (isset($_POST['submit'])) {
...
}
BTW, you can do that entire update in a single query.
UPDATE card_credit AS cc
CROSS JOIN card_refill AS cr
CROSS JOIN (SELECT * FROM card_credit ORDER BY id DESC LIMIT 1) AS cc1
SET cc.value = cr.col2 + cc1.col2
WHERE cr.refill = '$val1'
Like GolezTrol said from his comment. You're mixing object and functional notation.
Although this might not work exactly how you need it to because I don't have all the information. I have written you something I think is close to what you're looking for.
<?php
// Define the below connections via $username = ""; EXTRA....
// This is best done in a separate file.
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$val1 = $_POST['fill'];
$result1 = $conn->query("SELECT * FROM card_refill WHERE refill = '$val1' ");
$result2 = $conn->query("SELECT * FROM card_credit ORDER BY id DESC LIMIT 1:");
$refill1 = array(); // Pass Results1 Into Array
while($row = $result1->fetch_assoc()) {
$refill1[] = $row[2];
}
$refill = array(); // Pass Results2 Into Array
while($row = $result2->fetch_assoc()) {
$refill[] = $row[2];
}
/* Without an example of what data you are getting from your tables you will have to figure out what data you want from the arrays.
$money= $refill+$refill1;
echo "DEBUG: $money";
*/
// This code will not be functional until your populate the $money value.
$sql = "UPDATE card_credit SET value = '$money' ";
if ($conn->query($sql) === TRUE) {
echo nl2br("Record updated successfully"); // DEBUG
print_r(array_values($refill1)); // DEBUG
print_r(array_values($refill)); // DEBUG
echo nl2br("\n"); // DEBUG
} else { // DEBUG
echo "Error updating record: " . $conn->error; // DEBUG
echo nl2br("\n"); // DEBUG
}
$conn->close();
?>

Categories