Ok, I have changed the code to two different querys and cut the GET_$ID semicolon and place it like the isset POST variables.
Now I get two new errors:
Notice: Undefined index: edit in C:\wamp64\www\projekt\edit.php on line 20
"$ID = $_GET['edit']; "
Notice: Undefined variable: title in C:\wamp64\www\projekt\edit.php on line 23
"SET title='$title'" "ID = $ID"
So something is lost from the ID or POST.
I have dumped all the values so they exist, is there something wrong with the syntax?
if (isset($_POST['submit']))
{
// input array
if (isset($_POST['title'])
&& isset($_POST['director'])
&& isset($_POST['year'])
&& isset($_POST['genre'])
&& isset($_GET['edit']))
// filter txt input
$title = filter_input(INPUT_POST,'title',FILTER_SANITIZE_SPECIAL_CHARS);
$director = filter_input(INPUT_POST,'director',FILTER_SANITIZE_SPECIAL_CHARS);
$year = filter_input(INPUT_POST,'year',FILTER_SANITIZE_SPECIAL_CHARS);
$category = $_POST['genre'];
$ID = $_GET['edit'];
// Edit query
$sql = "UPDATE movies
SET title='$title' director='$director' year='$year'
WHERE ID='$ID'";
$insert = mysqli_insert_id($conn);
$insert = "UPDATE category
SET category='$category'
WHERE ID='$ID'";
if ($conn->query($sql) && $conn->query($insert) === TRUE) {
echo "<div class='receipt'>Record ".$ID." updated successfully<br>
<a href='index.php'>Gå tillbaka</a> </div>";
} else {
echo "Fel vid anslutning : " . $conn->error;
}
} $conn->close();
Related
Having a problem with the 2nd query. when it gets to mysql_fetch_row - it kicks back
mysqli_fetch_row() expects parameter 1 to be resource, boolean given
Now I have also tried fetch assoc. But that as well kicks back an error. I have tried doing the 2nd query with '$id' and without it, yet still nothing. I think i am missing something very obvious.
I have went through many of the common post on here that refer to the error I am given but still haven't found a solution.
$con1 = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
$con2 = mysqli_connect($dbhost,$dbuser2,$dbpass2,$dbname2);
// Check connections
if($con1)
{ echo("1st Connection Successful "); }
else
{ echo("1st Connection Failed "); }
if($con2)
{ echo("2nd Connection Successful "); }
else
{ echo("2nd Connection Failed "); }
//start first database query
$query = "SELECT id, email FROM ow_base_user ORDER by ID LIMIT 30";
$result = $con1->query($query);
while($row = $result->fetch_row()) {
$rows[]=$row;
$id = $row[0];
$email_row = $row[1];
$email_sub = substr($email_row, 0, 2);
if ( $email_sub == "dk" ) {
$email = "1"; }
elseif ( $email_sub == "sv" ) {
$email = "3"; }
elseif ( $email_sub == "no" ) {
$email = "4"; }
elseif ( $email_sub == "nl" ) {
$email = "5"; }
elseif ( $email_sub == "de" ) {
$email = "2"; }
elseif ( $email_sub == "fi" ) {
$email = "6"; }
else {}
if (isset($email)) {
//start 2nd database query
$query2 = "SELECT country_id FROM website_user WHERE userId = $id LIMIT 1";
$result2 = $con2->query($query2);
$row2 = mysqli_fetch_row($result2);
$country = $row2[6];
if (isset($country)) {
}
if (DEVMODE) {
echo "user id = ".$id." -- ";
echo "email = ".$email." -- ";
echo "COUNTRY = ".$country."<br />";
}
}
}
So changed the code around a bit to print out errors incase the query wasn't going through.
If($result2 === false) {
echo "error while executing mysql: " . mysqli_error($con2);
} else {
after adding this in I received the error.
mysqli_error() expects parameter 1 to be mysqli, null given
Ok so, these errors where caused by a typo in the databse table name thanks for pointing out the error report all method in the comments.
UPDATE: so after fixing the connection issue. I receive Notice: Undefined offset: 5 in $country = $row2[6];
UPDATE: after change $country = $row2[0]; - This solved the final issue.
for development build you can use
error_reporting(E_ALL);
ini_set('display_errors', 1);
which showed the database table was misspelled. after that, had to change the which row was being pulled since it was only 1 row, instead of all rows.
I am trying to delete an entry, But constantly getting these errors:
Notice: Undefined index: wineTypeID in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 33
Notice: Undefined index: wineName in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 34
Notice: Undefined index: wineSize in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 35
Notice: Undefined index: winePrice in
/storage/ssd5/765/3453765/public_html/wine_action.php on line 36
my code looks like this
<?php
// include the connection stream include 'connectYes.php';
// here are the two hiddent fields
// they tell the script is this is an add, update or delete
// and for an update and delete, they pass the id
$action = $_REQUEST["action"];
$id = $_REQUEST["id"];
$wineTypeID = $_REQUEST["wineTypeID"];
$wineName = $_REQUEST["wineName"];
$wineSize = $_REQUEST["wineSize"];
$winePrice = $_REQUEST["winePrice"];
if ($action == 'a') {
$query = "insert into WINE values (
null,
'$wineTypeID',
'$wineName',
'$wineSize',
'$winePrice'
)";
mysqli_query($conn,$query)
or die (mysqli_error());
print "<h3>The Item $wineName is added to the List</h3>";
" </h3>";
}
if ($action == 'u') {
$query = "update WINE
set wineTypeID = '$wineTypeID',
wineName = '$wineName',
wineSize = '$wineSize',
winePrice = '$winePrice'
where wineID = '$id'";
mysqli_query($conn, $query)
or die(mysqli_error());
print "<h3>Update Successful</h3>";
} // end u
if ($action == 'd') {
$query = "delete from WINE
where wineID = '$id'";
mysqli_query($conn, $query)
or die("query failed:" . mysqli_error());
print "<h3>Delete Successful</h3>";
}
?>}
Links to my page :- my page
Link to my files :- Php Files
mysql structure Image :-Mysql structure
Please check you request parameters whether "wineTypeID" paremeter is passing or not.
<?php
include "connection.php";
if(!isset($_SESSION))
{
session_start();
}
$cand1 = $_POST['cand1'];
$cand2 = $_POST['vice1'];
$sess = $_SESSION['SESS_NAME'];
if(!$cand1){
$error="<center><h4><font color='#FF0000'>Please fill empty fields</h4></center></font>";
include"student.php";
exit();
}
$cand1 = addslashes($cand1);
$cand1 = mysqli_real_escape_string($con,$cand1);
$sql = 'SELECT * FROM student WHERE username="'.$_SESSION['SESS_NAME'].'" AND status="VOTED"';
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result)==1){
$msg="<center><h4><font color='#FF0000'>You have already been voted, No need to vote again</h4></center></font>";
include 'student.php';
exit();
}
else{
$sql = 'UPDATE candidate SET votecount = votecount + 1 WHERE cand_id = "'.$_POST['cand1'].'" OR cand_id = "'.$_POST['vice1'].'"';
$sql2 = 'UPDATE student SET status="VOTED" WHERE username="'.$_SESSION['SESS_NAME'].'"';
$result = mysqli_query($con,$sql);
$result2 = mysqli_query($con,$sql2);
if(!$result && !$result2){
die("Error on mysql query".mysqli_error());
}
else{
$msg="<center><h4><font color='#FF0000'>Congratulation, you have made your vote.</h4></center></font>";
include 'student.php';
exit();
}
}
?>
the errors are at code including
$sess= $_SESSION['SESS_NAME']; and at username="'.$_SESSION['SESS_NAME'].'"
i have tried every possibility so can you check my code?
mainly the error is undefined index at $session[session_name]?
Are you sure the session 'SESS_NAME' is getting set? In the code above you are only trying to access its value. Undefined index is the error thrown when there is no session with that name.
Looks like to me you haven't declared the variable $_SESSION['SESS_NAME']. Try doing the following
var_dump($_SESSION);
This will show you all the defined variables in the session, if SESS_NAME isn't there then it hasn't been defined. Maybe you have failed to define it somewhere else?
I have a blog where I'm selecting the articles from a database using PHP. The problem is that becuase of my search terms I'm hitting an error. Here is my code:
<?php
if(isset($_GET["cat"])){
$cat = $_GET["cat"];
}else{
$cat = "all";
};
?>
<?php
if($cat == "all"){
$cat_var = "";
}else{
$cat_var = "WHERE cat = '$cat'";
}; // NOTE THIS LINE
?>
<?php
if(isset($_GET["issue"])){$issue = $_GET["issue"];}else{
$issue = "all";
};
?>
<?php
if($issue == "all"){
$issue_var = "";
$limit = 4;
}
else{
$issue_var = "AND issue = '$issue'"; // NOTE THIS LINE
$limit = 200;
};
?>
<?php
$count_posts_sql = "SELECT id FROM articles $cat_var $issue_var"; // NOTE THIS LINE
$count_posts_res = mysqli_query($con, $count_posts_sql);
$num_init_posts = mysqli_num_rows($count_posts_res);
//If None, Then Exit
if($num_init_posts == 0){
header("Location: /home");
exit();
}
...
?>
So my url would be http://website.com/articles/all/2015-10, which is what I want. However $cat_var & $issue_var is causing the error because it's selecting:
SELECT * FROM articles AND issue = '2015-10' // NO WHERE STATEMEMT IS SHOWN
How do I overcome this error?
You could get this going by sticking a WHERE 1=1 in
$count_posts_sql = "SELECT id FROM articles WHERE 1=1 $cat_var $issue_var"; // NOTE THIS LINE
This is because you start off with an AND value = 1 without starting the WHERE clause, which creates an invalid query.
Then take the WHERE out of this line and replacing it with an AND:
$cat_var = "AND cat = '$cat'";
You can initialize your where query string like this:
$where = 'WHERE 1 = 1 ';
and for there after you can concatenate depending on your inputs.
i have the following information displayed
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0)
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
?>
as you can see i have created a game_details page which will display that specific Game_id when the image is clicked
im having trouble understanding how to pull the data out from that game_id in sql on the other page.
here is my attempt on the game_details page
<?php
if (!isset($_GET['$game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id={$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['$game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
This attempt is giving me the "invalid category ID" error
Would appreciate help
There are a few issues with your code.
Let's start from the top.
['$game_id'] you need to remove the dollar sign from it in $_GET['$game_id']
Then, $row['$game_id'] same thing; remove the dollar sign.
Then, game_id={$game_id}' will throw a syntax error.
In your first body of code; you should also use proper bracing for all your conditional statements.
This one has none if (mysqli_num_rows($result) > 0) and will cause potential havoc.
Rewrites:
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0){
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
}
?>
Sidenote for WHERE game_id='{$game_id}' in below. If that doesn't work, remove the quotes from it.
WHERE game_id={$game_id}
2nd body:
<?php
if (!isset($_GET['game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id='{$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
Use error checking tools at your disposal during testing:
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php
You want to be using $_GET['gameid'] as that's the parameter you passed.
You are calling for game_id when the link to go to game_details.php has the variable gameid. Either change the parameter in the link to game_id or call for gameid in your $_GET['$game_id'].
Also, as Fred -ii- said, take out the dollar sign in $_GET['$game_id']