This question already has an answer here:
entered form data is not saving in mysql db?
(1 answer)
Closed 9 years ago.
Here is my code:
<?php
include('admin/class.php');
Here is my db connection:
$link = mysqli_connect("localhost", "root", "", "timesheet1234");
Here is the action for save button:
if(isset($_POST['save']))
{
$user=$_SESSION['user'];
$sel =$_POST["selpro"];
$mon =$_POST["mon"];
$tue =$_POST["tue"];
$wed =$_POST["wed"];
$thu =$_POST["thu"];
$fri =$_POST["fri"];
$sat =$_POST["sat"];
$sun =$_POST["sun"];
Checking whether the $user is in db are not:
$sql=mysqli_query($link,"select * from emp
where username='".$_SESSION['user']."'");
$res=mysqli_num_rows($sql);
Here it checks whether to insert are not:
if($res==0)
{
$sql1 = mysqli_query($link,"INSERT INTO emp SET username='$user',
project code='$sel',mon=$mon,tue=$tue,wed=$wed,
thu=$thu,fri=$fri,sat=$sat,sun=$sun");
Here the problem comes:
if($sql1){
echo "<script type='text/javascript'>";
echo "alert('TimeSheet Saved..!')";
echo "</script>";
echo "<script type='text/javascript'>";
echo "window.location='my_tm.php'";
echo "</script>";
}
else
{
echo "<script type='text/javascript'>";
echo "alert('Some Error Occured ! Retry..!')";
echo "</script>";
echo "<script type='text/javascript'>";
echo "window.location='my_tm.php'";
echo "</script>";
}
}
}
?>
Isnt the problem with if($res==0)? Because you check if the session user is in the database and store the number of rows in $res. So I would guess that you want to insert the query when $res==1 (one user) and not when there is no user.
Also normally you have complete control over your session variables. So once you store a user in there, you can trust that its there. Unless ofcourse you made mistakes on the authentication process.
Also in your javascript code you want to use window.location.href='my_tm.php to change the url and not just window.location
Try this as your insert query
$sql1 = mysqli_query("INSERT INTO emp SET username='$user',
`project code`='$sel',mon='$mon',tue='$tue',wed='$wed',
thu='$thu',fri='$fri',sat='$sat',sun='$sun'",$link);
The project code column has space
Related
This question already has answers here:
Use bound parameter multiple times
(5 answers)
Closed 3 years ago.
I now already create a table called' factory. In his table, there are two columns which is Fac_ID (Primary Key, Varchar) and Fac_Name (Varchar). Let say if I want to add a new factory. E.g F09, then the Fac_ID and Fac_Name also insert with F09.
I just only key in F09 at Fac_Name and after I click button 'add', it will save F09 at Fac_ID and Fac_Name. Below is my current PHP code
<?php
//including the database connection file
include_once("config.php");
if(isset($_POST['Submit'])) {
$Fac_Name = $_POST['Fac_Name'];
$Fac_ID = $_POST['Fac_Name'];
// checking empty fields
if(empty($Fac_ID)) {
if(empty($Fac_ID)) {
echo "<font color='red'>Name field is empty.</font>
<br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go
Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$sql = "INSERT INTO users(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_Name)";
$query = $conn->prepare($sql);
$query->bindparam(':Fac_Name', $Fac_Name);
$query->bindparam(':Fac_Name', $Fac_ID);
$query->execute();
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
?>
You cannot use the same bind parameter twice, so would suggest to do the following:
$sql = "INSERT INTO users(Fac_Name, Fac_ID) VALUES(:Fac_Name, :Fac_Id)";
$query = $conn->prepare($sql);
$query->bindparam(':Fac_Name', $Fac_Name);
$query->bindparam(':Fac_Id', $Fac_ID);
$query->execute();
It also makes more sense as the variable you put in is also called $Fac_ID.
First I'm hitting on a wall here and I really could use your help. I coded the database so I have it all up and working plus all the data inside. I worked the HTML and the CSS media print query and I have it how I want it to look exactly. All I have to do now is:
for every row of the mysql select table I have to fill every specific input form
of the html page I made and print it
Can someone give me a hint of how I can do that?
Assuming you want to connect to your database and simply fetch the id you can do the following.
Ensure you change my_host, my_user, my-password, my_databse,my_tablewith your configuration settings. Then if you want to fetch anything else thanid` just change it to the column name you are looking for.
Be aware we are using PHP here.
// Open Connection
$con = #mysqli_connect('my_host', 'my_user', 'my-password', 'my_databse');
if (!$con) {
echo "Error: " . mysqli_connect_error();
exit();
}
// Some Query
$sql = 'SELECT * FROM my_table';
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($query))
{
echo $row['id'];
}
// Close connection
mysqli_close ($con);
Check this link to get a in-depth explanation.
You can do this with two pages. One page gives you the overview and the other page gives you a print preview of your invoice.
The overview:
// DB select stuff here
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>\n";
echo " <td>".htmlspecialchars($row['what'])."</td>\n";
echo " <td>".htmlspecialchars($row['ever'])."</td>\n";
echo " <td>Detail</td>\n";
echo "</tr>\n";
}
The detail page:
$sql = 'SELECT your, columns FROM tab WHERE id = ?';
$stmt = $db->prepare($sql);
$stmt->execute(array($_GET['id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
echo "There is no data for the given Id\n";
return;
}
echo "What: ".htmlspecialchars($row['what'])."<br />\n";
echo "Ever: ".htmlspecialchars($row['ever'])."<br />\n";
In my PHP script, there are 3 echo, based on the conditionals
echo "Error"
echo "User already existed"
echo "Success"
One of the echo will be passed to my android apps, if (s.equals("Success")), where s is the echo message.
But the echo I got when the registration success is <br />, according to the logcat. For User already existed have no problem.
Since s is not Success, I can't start another activity which is depended on the string. Can anyone explain how the break tag got echoed? The registration information successfully inserted into database, though.
elseif ($roleID == "C") {
$sql6 = "SELECT runnerID FROM tr_customer WHERE customerID = '$newID'";
$check4 = mysqli_fetch_array(mysqli_query($con,$sql6));
if(!isset($check4)) {
// add into db
$customerID = $roleID . $newID;
$sql7 = "INSERT INTO tr_customer(customerID, name, phoneNo, locationID, roleID, email) VALUES ('$customerID', '$name', '$phoneNo', '$locationID', '$roleID', '$email')";
if(mysqli_query($con,$sql7)) {
echo "Success";
}
} else {
$newID = checkExist();
}
I would examine the code where the variable is defined. If you could post that part of the code as an edit then perhaps someone can review it for you as well. There is just not enough information here to discern where your error is coming from.
EDIT:
Consider changing the way you check for a successful update.
$result = mysqli_query($con,$sql7);
if(mysqli_num_rows($result) == 1){
echo "Success";
}
I'm quite new to mysql tables and I know I should use PDO or mysqli, but this is just an example for school.
Here's my problematic code:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ($uporab != $row['uporabnisko'] || $pass != $row['geslo']){
echo '<script language="javascript">';
echo 'alert("Uporabnisko ime ali geslo ni pravilno! Prosim poskusi znova.")';
echo '</script>';
header('Location: index1.php');
}
else{
header('Location: Stranzaindexom.html');
}
}
When I use "while" I get error popped out 5 times (I think it depends on how many values are in table in database), but when I don't use while, than I get checked just first id (first row) in table.. I checked for other answers but I haven't found the proper one..
Any help?
PS: connection to the database is successful, and taking values of table is good to.. So my problems just lay somewhere here.
May be you can try this
Why do we need to select all the records and check all the records the best is to pull only that record which is necessary.
$result = mysql_query("SELECT * name FROM usertable WHERE uporabnisko='$uporab' AND geslo = '$pass'");
if($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo '<script language="javascript">';
echo 'alert("Uporabnisko ime ali geslo ni pravilno! Prosim poskusi znova.")';
echo '</script>';
echo "<script language='javascript'> window.location='index1.php'; </script>";
}
else
{
echo "<script language='javascript'> window.location='Stranzaindexom.html'; </script>";
}
Think reverse check the positive case and give the alert on else like:
if ($uporab == $row['uporabnisko'] && $pass == $row['geslo']){
header('Location: Stranzaindexom.html');
exit();
} else {
echo '<script language="javascript">';
echo 'alert("Uporabnisko ime ali geslo ni pravilno! Prosim poskusi znova.")';
echo '</script>';
}
Or use the username and password at the sql and give limit 1 so you dont need a while becouse it garanteus that result set will be 1 row or 0 row:
mysql_query("SELECT * FROM users WHERE username='".$escapedUsername."' AND password='".$escapedPassword."' LIMIT 1");
Here's my code. Is there some reason this should not work? I'm getting all of the fields from MySQL.
Basically what I want is to send information back from a page, with an id number, and this should be used to select the row number for MySQL.
Here's my code from the first page:
$org = $_POST['organization'];
header('Location: '.$admin.'?org='.$org);
And then my code on main page:
ECHO '<script>';
ECHO 'document.getElementById("orgid").value="'.$org_id.'"';
ECHO 'document.getElementById("orgname").value="'.$org_name.'"';
ECHO 'document.getElementById("add1").value="'.$add_1.'"';
ECHO 'document.getElementById("add2").value="'.$add_2.'"';
ECHO 'document.getElementById("city").value="'.$city.'"';
ECHO 'document.getElementById("state").value="'.$state.'"';
ECHO 'document.getElementById("zip").value="'.$zip.'"';
ECHO 'document.getElementById("url").value="'.$url.'"';
ECHO 'document.getElementById("email").value="'.$email.'"';
ECHO 'document.getElementById("phone").value="'.$phone.'"';
ECHO 'document.getElementById("contact").value="'.$contact.'"';
ECHO 'document.getElementById("hours").value="'.$hours.'"';
ECHO 'document.getElementById("file").value="'.$file.'"';
ECHO 'document.getElementById("notes").value="'.$notes.'"';
ECHO 'document.getElementById("description").value="'.$description.'"';
ECHO '</script>';
And here's the code to communicate with MySQL:
if (isset($_GET["org"]) && ($_GET['org'] !== '')) {
$org = $_GET['org'];
$resorgfull = mysql_query("SELECT org_id, org_name, add_1, add_2, city, state, zip, url, email, phone, contact, hours, file_loc, notes, description FROM organization WHERE org_id=".$org.");
if (!$resorgfull) {
die('Invalid query: ' . mysql_error());
}
One thing definitely wrong is the way you're echoing the scripts, it should be more like this:
echo "<script type='text/javascript'>\n";
echo "document.getElementById('orgid').value='$org_id';\n";
...
Or preferably...
// Close the PHP tag and output straight HTML with embedded PHP values:
?>
<script type="text/javascript">
document.getElementById('orgid').value='<?php echo $org_id; ?>';
...
If this doesn't fix it, you'll have to give us more info regarding what is not working.
I don't see the code where you take the query result and fetch each row.. are you doing that?