I have to make a site for school. It need to be linked with a database. On localhost everything is ok and works, but when I tried to upload it on the host of the school, I get this error:
Notice: Undefined index: vragen in /mnt/studentenhomes/arnaud.gandibleux/public_html/datamanagement/index.php on line 44
I can't find the solution
index.php
<div id="tekst">
<table align='center'>
<?php
//if (isset($_GET['vragen'])){
if ($_GET['vragen'] === 'Alleclubs') {
getclubs();
} elseif ($_GET['vragen'] === 'ledenvjf') {
getVJFleden();
echo "test";
} elseif ($_GET['vragen'] === 'ledenffbj') {
getFFBJleden();
}
elseif (isset($_GET['clubnr'])) {
getLedenPerClubEnID($_GET['clubnr']);
}
else{
getclubs();
}
// }
?>
</table>
Crud.php
function getVJFleden() {
global $mysqli;
$result = $mysqli->query("SELECT * FROM Leden l JOIN Clubs c ON l.clubnr = c.clubnr join Bonden b
ON b.ID_bond = c.ID_bond LEFT JOIN adressen a ON a.ID_adress = l.ID_adress WHERE b.naam_bond = 'VJF';");
if ($result) {
if ($result->num_rows > 0) {
echo"<caption>Alle leden VJF</caption>";
echo "<th>Voornaam</th><th>achternaam</th><th>leeftijd</th><th>Kye</th><th>adress</th>";
while ($leden = $result->fetch_object()) {
echo "<tr><td>$leden->lid_voornaam</td> ";
echo "<td>" . $leden->lid_achternaam . "</td> ";
echo "<td>" . $leden->lid_leeftijd . "</td> ";
echo "<td>" . $leden->kye . "</td> ";
echo "<td>" . $leden->straatnaam . " " . $leden->huisnummer . " " . $leden->postcode . " " . $leden->gemeente . "</td> ";
echo "<td><form id='update' action='update.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='hidden' name='clubnr' value='" . $_GET['clubnr'] . "'/>
<input type='image' src='update.png' alt='Update' width='22' height='22'>
</form>
<form id='delete' action='deleteLid.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='image' src='delete.png' alt='detele' width='22' height='22'>
</form></td> ";
}
}
}
$mysqli->close();
}
function getFFBJleden() {
global $mysqli;
#, Adressen a
#AND l.ID_adress = a.ID_adress
$result = $mysqli->query("SELECT * FROM Leden l JOIN Clubs c ON l.clubnr = c.clubnr join Bonden b ON b.ID_bond = c.ID_bond LEFT JOIN adressen a ON a.ID_adress = l.ID_adress WHERE b.naam_bond = 'FFBJ';");
if ($result) {
if ($result->num_rows > 0) {
echo"<caption>Alle leden VJF</caption>";
echo "<th>Voornaam</th><th>achternaam</th><th>leeftijd</th><th>Kye</th><th>adress</th>";
while ($leden = $result->fetch_object()) {
echo "<tr><td>$leden->lid_voornaam</td> ";
echo "<td>" . $leden->lid_achternaam . "</td> ";
echo "<td>" . $leden->lid_leeftijd . "</td> ";
echo "<td>" . $leden->kye . "</td> ";
echo "<td>" . $leden->straatnaam . " " . $leden->huisnummer . " " . $leden->postcode . " " . $leden->gemeente . "</td> ";
echo "<td><form id='update' action='update.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='hidden' name='clubnr' value='" . $_GET['clubnr'] . "'/>
<input type='image' src='update.png' alt='Update' width='22' height='22'>
</form>
<form id='delete' action='deleteLid.php' method='POST'>
<input type='hidden' name='id' value='" . $leden->ID_lid . "'/>
<input type='image' src='delete.png' alt='detele' width='22' height='22'>
</form></td> ";
}
}
}
$mysqli->close();
}
You need to ensure that the array index exists before you try to use it. Being that it is a $_GET variable, it may not have been passed as a URL parameter.
Uncomment
//if (isset($_GET['vragen'])){
To
if (isset($_GET['vragen'])){
Two things are happening:
you have Notice warnings on. See here on how to turn them off: (How do I turn off PHP Notices?)
It's probably one of the $_GET parameters that you didn't specify on the url. So when you load the page, you're accessing the $_GET superglobal, but there may be nothing in it, and you're trying to print out that value.
You can either turn off notices or use a function to get a values from $_GET, and in this function check that the value isset() first before you access it.
when you visit the site you do something like: mysite.com?vragen=ledenvjf
if you forget the part after the ? there is no $_GET['vragen'] and the notice is thrown
this line
//if (isset($_GET['vragen'])){
prevents the notice from being thrown you should uncomment it together with this line:
// }
It means that variable vragen is not set so your $_GET array doesn't have any element with index vragen.
Uncomment //if (isset($_GET['vragen'])){ and // } since this was checking if $_GET variable with name vragen was set. Example of when this check will pass index.php?vragen=ledenvjf
Related
I'm a beginner with PHP. I watched a tutorial to create a form which modifies my wamp-created mysql database table. Copied the video at first, but then made my own table from scratch and tried to upgrade it.
My add row works correctly, but the update and remove do not. I think the WHERE clause is not correct, referencing reg_id.
I created a unique primary key, which auto-increments and cannot be modified; this is what I want to reference when changes are made (since it cannot be changed).
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE register SET First_Name='$_POST[first_name]', Last_Name='$_POST[last_name]', Breed='$_POST[breed]', Weight='$_POST[weight]', Age='$_POST[age]', Sex='$_POST[sex]' WHERE '$_POST[reg_id]'='$_POST[reg_id]'";
mysqli_query($con,$UpdateQuery);};
if (isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM register WHERE reg_id='$_POST[reg_id]'";
mysqli_query($con,$DeleteQuery);};
Here is the rest of it where the form is located:
while($record=mysqli_fetch_array($myData)){
echo "<form action=register.php method=post>";
echo "<tr>";
echo "<td>" . $record['reg_id'] . " </td>";
echo "<td>" . "<input type=text name=first_name value=" . $record['First_Name'] . " </td>";
echo "<td>" . "<input type=text name=last_name value=" . $record['Last_Name'] . " </td>";
echo "<td>" . "<input type=text name=breed value=" . $record['Breed'] . " </td>";
echo "<td>" . "<input type=int name=weight value=" . $record['Weight'] . " </td>";
echo "<td>" . "<input type=int name=age value=" . $record['Age'] . " </td>";
echo "<td>" . "<input type=text name=sex value=" . $record['Sex'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
Please help me fix it.
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE register SET First_Name='".$_POST['first_name']."',Last_Name='".$_POST['last_name']."', Breed='".$_POST['breed']."', Weight='".$_POST['weight']."', Age='".$_POST['age']."', Sex='".$_POST['sex']."' WHERE reg_id ='".$_POST['reg_id']."'";
mysqli_query($con,$UpdateQuery);
};
if (isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM register WHERE reg_id='".$_POST['reg_id']."'";
mysqli_query($con,$DeleteQuery);
};
should be enclosed by ' ,that is optional.add hidden will be more better
echo "<td><input type='hidden' name='reg_id' value='".$record['reg_id']."'></td>";
echo "<td><input type='submit' name='update' value='update'></td>";
echo "<td><input type='submit' name='delete' value='delete'></td>";
You are using $_POST without '.
Try this : {$_POST['first_name']} and replace all $_POST according to this.
So your update query will be like this :
"UPDATE register SET First_Name='{$_POST['first_name']}', Last_Name='{$_POST['last_name']}', Breed='{$_POST['breed']}', Weight='{$_POST['weight']}', Age='{$_POST['age']}', Sex='{$_POST['sex']}' WHERE reg_id='{$_POST['reg_id']}'";
There is no field with name reg_id, so your $_POST['reg_id'] will not work.Also please change your where condition. You are matching same value in where condition.
And your delete query will be :
"DELETE FROM register WHERE reg_id='{$_POST['reg_id']}'";
Your query is open for sql injection. Refer this :How can I prevent SQL injection in PHP?
display page
while($record = mysqli_fetch_array($myData)) {
echo "<table>";
echo "<tr>";
echo "<td>".$record['reg_id']."</td>";
echo "<td>".$record['First_Name']."</td>";
echo "<td>".$record['Last_Name']."</td>";
echo "<td>".$record['Breed']."</td>";
echo "<td>".$record['Weight']."</td>";
echo "<td>".$record['Age']."</td>";
echo "<td>".$record['Sex']."</td>";
echo "<td><a href='edit.php?reg_id=".$record['reg_id']."'>EDIT</a></td>";
echo "<td><a href='delete.php?reg_id=".$record['reg_id']."'>DELETE</a></td>";
echo "</tr>";
echo "</table>";
}
delete.php
<?php
if (isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM `register` WHERE `reg_id`={$_GET['reg_id']}'";
mysqli_query($con,$DeleteQuery);
header("Location: your display page");
};
?>
Edit Form
while($record = mysqli_fetch_array($myData)) {
echo '<form action="edit.php" method="Post">
<input type="text" name="First_Name" value="'.$record['reg_id'].'"/>
<input type="text" name="First_Name" value="'.$record['First_Name'].'"/>
<input type="text" name="Last_Name" value="'.$record['Last_Name'].'"/>
<input type="text" name="Breed" value="'.$record['Breed'].'"/>
<input type="text" name="Weight" value="'.$record['Weight'].'"/>
<input type="text" name="Age" value="'.$record['Age'].'"/>
<input type="text" name="Sex" value="'.$record['Sex'].'"/>
<imput type="submit" value="save" name="submit" />
</form>';
}
edit.php
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE `register` SET `First_Name`='{$_POST['first_name']}', `Last_Name`='{$_POST['last_name']}', `Breed`='{$_POST['breed']}', Weight='{$_POST['weight']}', `Age`={$_POST['age']}, Sex='{$_POST['sex']}' WHERE `reg_id`={$_GET['reg_id']}";
mysqli_query($con,$UpdateQuery);
header("Location: your display page");
};
I decided to make my database with an active hyperlink so when I find a record I click on an email address and and email is opened. BUT I have a problem with update, When I want to make a correction to the entry and when I press update the email disappears and I get:
Notice: Undefined index: Mail in /Applications/XAMPP/xamppfiles/htdocs/robocze/mydata_dodaj_test_1.php on line 36
I can add records but I can't update...
Can You please help me, thank You
the code:
<?php
session_start();
if(!isset($_SESSION["sess_user"])){
header("location:login.php");
} else {
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Baza Klientów</title>
</head>
<body>
<h3>Welcome, <?=$_SESSION['sess_user'];?>! Logout </h3>
<input type="button" onclick="location.href='mydata_dodaj_test_1.php';" value="Powrót do wyszukiwania" />
<input type="button" onclick="location.href='index.php';" value="MENU powrót" />
<h2><b><center>Wyszukiwanie Klientów ITalents</center></b></h2>
<?php
}
?>
<?php
$con = mysql_connect("","","","");
if (!$con){
die("Błąd połączenia: " . mysql_error());
}
mysql_select_db("baza",$con);
if(isset($_POST['update'])) {
$UpdateQuery = "UPDATE Klienci SET id='$_POST[id]', Firma='$_POST[Firma]', Mail='$_POST[Mail]', Data='$_POST[Data]', Konsultant='$_POST[Konsultant]' WHERE id='$_POST[hidden]'";
mysql_query($UpdateQuery,$con);
};
if(isset($_POST['add'])) {
$AddQuery = "INSERT INTO Klienci (id, Firma, Mail, Data, Konsultant) VALUES ('$_POST[uid]','$_POST[uFirma]','$_POST[uMail]','$_POST[uData]','$_POST[uKonsultant]')"; // this is the 36 line
mysql_query($AddQuery,$con);
};
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM Klienci WHERE CONCAT(Firma, Mail, Konsultant) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM Klienci ORDER BY id ASC";
$search_result = filterTable($query);
}
function filterTable($query)
{
$con = mysql_connect("","","","");
if (!$con){
die("Błąd połączenia: " . mysql_error());
}
mysql_select_db("baza",$con);
$filter_Result = mysql_query($query, $con);
return $filter_Result;
};
echo "<form action=mydata_dodaj_test_1.php method=post>";
echo "<input type=text name=valueToSearch placeholder=wpisz>";
echo "<input type=submit name=search value=Szukaj>";
echo "<table align=center style=text-align:center border=5>
<tr>
<th>ID</th>
<th>Firma</th>
<th>Mail</th>
<th>Data</th>
<th>Konsultant</th>
</tr>";
while($row = mysql_fetch_array($search_result)) {
echo "<form action=mydata_dodaj_test_1.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=int name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=varchar name=Firma value=" . $row['Firma'] . " </td>";
echo "<td>" . "<a href='mailto:{$row['Mail']}'>" . $row['Mail'] . " </td>";
echo "<td>" . "<input type=date name=Data value=" . $row['Data'] . " </td>";
echo "<td>" . "<input type=varchar name=Konsultant value=" . $row['Konsultant'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "</form>";
}
echo "<form action=mydata_dodaj_test_1.php method=post>";
echo "<tr>";
echo "<td><input type=text name=uid></td>";
echo "<td><input type=varchar name=uFirma></td>";
echo "<td><input type=text name=uMail></td>";
echo "<td><input type=text name=uData></td>";
echo "<td><input type=text name=uKonsultant></td>";
echo "<td>" . "<input type=submit name=add value=dodaj" . " </td>";
echo "</form>";
echo "</table>";
?>
<input type="button" onclick="location.href='mydata_dodaj_test_1.php';" value="Powrót do wyszukiwania" />
<input type="button" onclick="location.href='index.php';" value="MENU powrót" />
</body>
</html>
As error stated undefined index in your update statement
$UpdateQuery = "UPDATE Klienci SET id='$_POST[id]', Firma='$_POST[Firma]', Mail='$_POST[Mail]', Data='$_POST[Data]', Konsultant='$_POST[Konsultant]' WHERE id='$_POST[hidden]'";
you are accessing $_POST[Mail] which is no where in your form.
This question already has answers here:
Why would $_FILES be empty when uploading files to PHP?
(22 answers)
Closed 8 years ago.
I am trying to build a file uploader, with a youtube tutorial that doesn't cover files, and right now I am stuck on the name. Using $_Files returns nothing and I am unsure as to why. :/
I tried to echo it out, but nothing comes back.
Everything else seems to work though.
<html>
<head>
</head>
<body>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$con = mysql_connect("localhost","root","root");
if (!$con){
die("Can not connect: " . mysql_error());
}
mysql_select_db("example",$con);
if(isset($_POST['update'])){
$UpdateQuery = "UPDATE repo SET location='$_POST[location]', name='$_POST[name]', description='$_POST[description]' WHERE location='$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};
if(isset($_POST['delete'])){
$DeleteQuery = "DELETE FROM repo WHERE location='$_POST[hidden]'";
mysql_query($DeleteQuery, $con);
};
if(isset($_POST['add'])){
$AddQuery = "INSERT INTO repo (name, id, image, location, partners, description, date) VALUES ('$image_name', '','$_POST[uimage]', '$_POST[ulocation]', '$_POST[upartners]', '$_POST[udescription]', NOW())";
mysql_query($AddQuery, $con);
};
$sql = "SELECT * FROM repo";
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Image</th>
<th>Name</th>
<th>Location</th>
<th>Partners</th>
<th>Description</th>
<th>Date</th>
</tr>";
while($record = mysql_fetch_array($myData)){
?>
<form action="mydata5.php"
method="post" enctype="multipart/form-data">
<?php
echo "<tr>";
echo "<td>" . "<img src=Assets/Images/" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=topic value=" . $record['name'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $record['location'] . " </td>";
echo "<td>" . "<input type=text name=name value=" . $record['partners'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['description'] . " </td>";
echo "<td>" . "<input type=text name=description value=" . $record['date'] . " </td>";
echo "<td>" . "<input type=hidden name=hidden value=" . $record['location'] . " </td>";
echo "<td>" . "<input type=submit name=update value=update" . " </td>";
echo "<td>" . "<input type=submit name=delete value=delete" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "<form action=mydata5.php method=post>";
echo "<tr>";
// echo "<td><input type=file name=uimage></td>";
?>
<td><input type="file" name="uimage" id="uimage"></td>
<?php
$file = $_FILES['uimage']['tmp_name'];
$image_name = mysql_real_escape_string($_FILES['uimage']['name']);
echo $_FILES['uimage']['error'];
echo "<td><input type=hidden name=uname></td>";
echo "<td><input type=text name=ulocation></td>";
echo "<td><input type=text name=upartners></td>";
echo "<td><input type=text name=udescription></td>";
echo "<td>" . "<input type=submit name=add value=add" . " </td>";
echo "</form>";
echo "</table>";
mysql_close($con);
?>
</body>
</html>
You need to add enctype="multipart/form-data" in the form tag.
For file type fields you need to add enctype attribute in your form so that uploaded files can be access using $_FILES
Update form starting tag with below
<form action="mydata5.php"
method="post" enctype="multipart/form-data">
Check your Insert Query.You are using $_POST for image name. That is wrong.
so I have this code that other Stack members have helped me fine tune and correct some errors, the code all works as it should but there is one small detail, after successfully editing one record and clicking the update button ALL of the existing records that are in the database load into the page. Here is my code below:
<?php
$con = mysql_connect("localhost", "root", "M1q2w3e4r");
if (!$con) {
die("Can not connect: " . mysql_error());
}
mysql_select_db("inventory",$con);
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
};
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
$sql = "SELECT * FROM invoice".$where;
$myData = mysql_query($sql,$con);
echo "<table border=1>
<tr>
<th>Inv #</th>
<th>From</th>
<th>To</th>
<th>Type</th>
<th>Notes</th>
</tr>";
while($record = mysql_fetch_array($myData)){
echo "<form action='edit.php' method='post'>";
echo "<tr>";
echo "<td>" . "<input type='text' name='inv_number' value='" . $record['inv_number'] . "'> </td>";
echo "<td>" . "<input type='text' id='from' name='from_date' value='" . $record['from_date'] . "'> </td>";
echo "<td>" . "<input type='text' id='to' name='to_date' value='" . $record['to_date'] . "'> </td>";
echo "<td>" . "<input type='text' name='date_type' value='" . $record['date_type'] . "'> </td>";
echo "<td>" . "<input type='text' name='notes' value='" . $record['notes'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='id' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='hidden' name='hidden' value='" . $record['id'] . "'> </td>";
echo "<td>" . "<input type='submit' name='update' value='update'>" . " </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysql_close($con);
?>
I know it has to do with the form action="edit.php", as it refreshes the page the id number in the url is pulled out. So I tried this:
echo "<form action='edit.php?edit=<?php echo $_REQUEST["id"]; ?>' method='post'>";
but this only led to my edit.php to display as a blank page. If anyone can help me figure out how to prevent all the database records from being displayed in the page after clicking the update button it would really help.
I might do this, for example, if I just wanted to show the record that was just updated:
if (isset($_POST['update'])){
$UpdateQuery = "UPDATE invoice SET `inv_number`='$_POST[inv_number]', `from_date`='$_POST[from_date]', `to_date`='$_POST[to_date]',`date_type`='$_POST[date_type]', `notes`='$_POST[notes]' WHERE id='$_POST[id]'";
mysql_query($UpdateQuery, $con);
$where = ' where id='.$_POST[id];
}
else {
$where = '';
if(!empty($_GET) && !empty($_GET['edit'])) {
$where = ' where id='.$_GET['edit'];
}
}
You could also use REQUEST instead of GET and make a hidden input field with the name "edit" in your form.
Dont mind the security issues, this is just local testing, but when ever i click the update button none of the changes go through on the page or on the query and i get no erros.
<?php
$link = mysqli_connect("localhost", "root", "", "test") or die("could not connect");
if (isset($_POST['update'])) {
$updateQuery = (" UPDATE `test1` SET f_name = '$_POST[f_name]', l_name='$_POST[l_name]', email='$_POST[email]' WHERE id='$_POST[id]'");
mysqli_query($link, $updateQuery);
};
$query = ("SELECT * FROM `test1`");
$result = mysqli_query($link, $query);
echo "<table border=1
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
?>
change your form to
while($row = mysqli_fetch_array($result)) {
echo "<form method=post action=update.php>";
echo "<input type=hidden name=update>";
echo "<tr>";
echo "<td>" . "<input type=text name=f_name value=" . $row['f_name'] . " </td>";
echo "<td>" . "<input type=text name=l_name value=" . $row['l_name'] . " </td>";
echo "<td>" . "<input type=text name=email value=" . $row['email'] . " </td>";
echo "<td>" . "<input type=hidden name=id value=" . $row['id'] . " </td>";
echo "<td>" . "<input type=submit name=submit value=update" . " </td>";
echo "</tr>";
}
POST keys should be in quotes. Try this instead:
$updateQuery = "UPDATE test1 SET f_name = ".$_POST['f_name'].", l_name=."$_POST['l_name'].", email=".$_POST['email']." WHERE id=".$_POST['id'];
Try this:
$updateQuery = ("UPDATE `test1` SET f_name = '{$_POST['f_name']}', l_name='{$_POST['l_name']}', email='{$_POST['email']}' WHERE id='{$_POST['id']}'");
Also you can try echoing something inside your if (isset($_POST['update'])) { to make sure it is testing true.
Here is your problem:
if (isset($_POST['submit']) && $_POST['submit'] == 'update') {
The name of the submit button is submit not update, the value is update.