I have made this codes that registers the students using their learner id. And of course i wanted to make sure that there will be no duplication of learner id as students registered.These are my codes.
$select_query="select student_id from student_information where learner_id = '$learner_id'";
$result_set = mysql_query($select_query,$link_id);
if($row = mysql_fetch_array($result_set)){
$flag="exists";
header("location:Student_login.php?flag=$flag&student_id=$student_id");
die();
}
else{
/*
This block is used to insert the learners record in database
if the student_id is not yet registered in the database.
*/
mysql_query("SET AUTOCOMMIT = 0 ");
if(mysql_error() != null){
die(mysql_error());
}
Everytime there is a duplication of learner id. I am using this code on the other page for notifying me that the learner id exist already.
>This Learner Id( <?php $_GET['learner_id'] ?> ) already exists. Please try again with another Learner Id.</td>
The log in page give me notice
This Learner Id(Notice: Undefined index: learner_id in C:\xampp\htdocs\a\Student_login.php on line 61
) already exists. Please try again with another Learner Id.
How do i fix this one. Please advise.
In your first program where you get $learner_id. Are you declared or it is constant?
Did you use the below code above select statement.
$learner_id=$_GET['learner_id']
//or
$learner_id=$_POST['learner_id']
Considering it's a _GET, you can't be sure it's always there - unlike a constant.
if( array_key_exists('learner_id', $_GET) ) {
$_GET['learner_id'] = ctype_digit( (string) $_GET['learner_id']) ? (int) $_GET['learner_id'] : 0;
} else {
$_GET['learner_id'] = 0;
}
Check it exists
Check it doesn't contain any dodgy characters
If it does, reset the value to 0
If it doesn't exist, create it with a value of 0
0 value will of course fail - as you won't have index 0 in your database ;)
Tip: Never trust user input. Validate and sanitize data input ;)
Looking at your header, you're not including learner_id in the query string.
header("location:Student_login.php?flag=$flag&student_id=$student_id");
Change to;
header("location:Student_login.php?flag=$flag&learner_id=$student_id");
How are you getting this $learner_id (using GET or POST)?
//if
$learner_id=$_REQUEST['learner_id'];
$select_query="select student_id from student_information where learner_id = '$learner_id'";
$result_set = mysql_query($select_query,$link_id);
$num_rows = mysql_num_rows($result_set);
if($num_rows>0){
$row = mysql_fetch_array($result_set);
$flag="exists";
header("location:Student_login.php?flag=$flag&student_id=$student_id");
die();
}
else{
mysql_query("SET AUTOCOMMIT = 0 ");
if(mysql_error() != null){
die(mysql_error());
}
}
Related
I have a problem with this line of code - I have spent most of the day trying to get this resolved - can any one help?
Here is the code that is causing the problem form what I can see! The problem is around the $qry...
$qry = "INSERT INTO members (employer, flat) VALUES('$employ','$address') WHERE login='$_login'";
$result = #mysql_query($qry);
//Check whether the query was successful or not
if($result) {
header("location: member-profile.php");
exit();
}else {
die("Query failed");
}
?>
ERROR showing is:
( ! ) Notice: Undefined variable: _login in C:\wamp\www\123456\update.php on line 67
Thanks all.
First variable $_login is Undefined, and second, it seems you are trying to update.
You do not user WHERE for SELECT query.
If you want to update, it then your query should very much like this:
$sql = 'UPDATE table SET username = '$username' WHERE id = $_login;
variable $_login means, pretty much, the variable $_login is not defined. You must give it a value, before you can you expect it to work in your query.
INSERT doesn't allow the WHERE attribute, you need to use UPDATE instead
"UPDATE members SET employer='$employ', flat='$address' WHERE login='$_login'"
Be sure to prevent SQL injections and since mysql_* functions are deprecated you should switch to MySQLi or PDO
As for the error itself, you need to check if the variables are defined that you are using
if (!isSet($_login))
// do sth
I am new to PHP. And I'm trying to prevent duplicates through php script. I use wamp server. here's the code,
$query1="select COUNT(email) from customer_info where email=$eml";
$res1=mysqli_query($db_Con,$query1);
$row=mysqli_fetch_assoc($res1);
I wanted to know if $row here has the count value. i.e If xyz#abc.com already exists(one occurance) in "customer_info", wil $row be having the value "1"??
And what exactly should be passed to mysqli_fetch_assoc() ? I got a warning saying it expects "mysqli_result" , not a boolean.
P.S: I don't want to make the column email in my table as UNIQUE.
Thanks!
try this solution
$query = "SELECT COUNT(email) AS res FROM customer_info WHERE email = $eml"; // res is a alias... can be any unreserved word
$st = mysqli_query($db_con, $query);
$row = mysqli_fetch_object($st); //mysqli_fetch_object bind the resultset as object into the variable as described here http://php.net/mysqli_fetch_object
// $row->res is a object with property "res" from your query
if($row->res > 0)
{
//has an e-mail
}
else
{
///
}
i have a problem my script has three mysql_query which should be used after each other , i am trying to create a script that reserve tickets by changing their status from sold = "No" to "Yes", the script count the number of tickets user has entered on html form which give the server side a variable with number of tickets called = $tickets.
hint : this is such a model so no need for mysql injection security
here is my code :
//get ticket status
$eventTicket = mysql_query("SELECT eventTickets FROM beventreservation WHERE eventId = '$eventId'") or die(mysql_error());
$ticketrow = mysql_fetch_array($eventTicket) or die(mysql_error());
//test... which is working !
echo $ticketrow['eventTickets'];
//get classId from classes
$selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());
$classrow = mysql_fetch_array($selectClass) or die(mysql_error());
//this var is to define which class the user used
$choosedClass = $classrow['classId'];
//test ... which did not work !!!
echo $classrow['classId'];
if ($ticketrow['eventTickets'] == "Yes")
{
for($counter=1;$counter<$numberOfTickets;$counter++)
{
$bookTicket = mysql_query("UPDATE unites SET ticketSold = 'Yes' WHERE businessreservationIdFk = '$eventId' AND classIDfk ='$choosedClass'") or die(mysql_error());
echo "ticket ". $counter . " done !";
}
}
the script doesn't fetch this syntax, and there is no errors showed on my page !
$classrow = mysql_fetch_array($selectClass) or die(mysql_error());
also , i tried to echo the variable $tickets after this syntax , it did not showed up, is there a problem to fetch more than mysql_query on the same script page ? tell me where do i go wrong here please .
Don't call die() in conjunction with a mysql_fetch_*() call. If there are no rows returned, mysql_fetch_array() returns FALSE, which triggers your die() and kills your script even though there was no error. Since you have already don error checking on $selectClass in the mysql_query() call, you know it has succeeded.
// This query returned no rows, but was successful syntactically and functionally.
$selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());
Instead, test if rows were returned:
if (mysql_num_rows($selectClass) > 0) {
// Fetch and do other stuff
$classrow = mysql_fetch_array($selectClass);
$choosedClass = $classrow['classId'];
// etc...
// etc...
}
else {
// Do whatever you need to do if no rows return
}
I am getting this PHP error, what does it mean?
Notice: Undefined offset: 0 in
C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
From this code:
<?php
include("config.php");
function getAllVotes($id)
{
$votes = array();
$q = "SELECT * FROM entries WHERE id = $id";
$r = mysql_query($q);
if(mysql_num_rows($r)==1)//id found in the table
{
$row = mysql_fetch_assoc($r);
$votes[0] = $row['votes_up'];
$votes[1] = $row['votes_down'];
}
return $votes;
}
function getEffectiveVotes($id)
{
$votes = getAllVotes($id);
$effectiveVote = $votes[0] - $votes[1]; //ERROR THROWN HERE
return $effectiveVote;
}
$id = $_POST['id'];
$action = $_POST['action'];
//get the current votes
$cur_votes = getAllVotes($id);
//ok, now update the votes
if($action=='vote_up') //voting up
{
$votes_up = $cur_votes[0]+1; //AND ERROR THROWN HERE
$q = "UPDATE threads SET votes_up = $votes_up WHERE id = $id";
}
elseif($action=='vote_down')
{
$votes_down = $cur_votes[1]+1;
$q = "UPDATE threads SET votes_down = $votes_down WHERE id = $id";
}
$r = mysql_query($q);
if($r)
{
$effectiveVote = getEffectiveVotes($id);
echo $effectiveVote." votes";
}
elseif(!$r) //voting failed
{
echo "Failed!";
}
?>
You are asking for the value at key 0 of $votes. It is an array that does not contain that key.
The array $votes is not set, so when PHP is trying to access the key 0 of the array, it encounters an undefined offset for [0] and [1] and throws the error.
If you have an array:
$votes = array('1','2','3');
We can now access:
$votes[0];
$votes[1];
$votes[2];
If we try and access:
$votes[3];
We will get the error "Notice: Undefined offset: 3"
first, check that the array actually exists, you could try something like
if (isset($votes)) {
// Do bad things to the votes array
}
This answer helped me https://stackoverflow.com/a/18880670/1821607
The reason of crush — index 0 wasn't set. Simple $array = $array + array(null) did the trick. Or you should check whether array element on index 0 is set via isset($array[0]). The second variant is the best approach for me.
Use print_r($votes); to inspect the array $votes, you will see that key 0 does not exist there. It will return NULL and throw that error.
getAllVotes() isn't returning an array with the indexes 0 or 1. Make sure it's returning the data you want by calling var_dump() on the result.
I encountered this as well and the solution is simple, dont hardcode the array index position in your code.
Instead of
$data[0]['somekey'] do
foreach($data as $data_item)
{
echo $data_item['somekey'];
} If there is an array or more you can perform your desired action inside the loop, but if it's undefined it will not bring an error. you can also add other checks on top of that.
You could also add a variable and increment it in a for in loop to limit your looping if you want only the first positions or something.
As explained it happens because there is no data in the $cur_votes[0] and hence it throws an error.
To ensure your code works fine, before performing "$votes_up = $cur_votes[0]+1;" echo the $cur_votes[0] value to see if there is any value stored or not.
Surely, there is no value stored.
function getEffectiveVotes($id)
According to the function header, there is only one parameter variable ($id).
Thus, on line 27, the votes[] array is undefined and out of scope. You need to add another
parameter value to the function header so that function getEffectiveVotes() knows to expect two parameters. I'm rusty, but something like this would work.
function getEffectiveVotes($id, $votes)
I'm not saying this is how it should be done, but you might want to research how PHP
passes its arrays and decide if you need to explicitly state to pass it by reference
function getEffectiveVotes($id &$votes) <---I forget, no time to look it up right now.
Lastly, call function getEffectiveVotes() with both arguments wherever it is supposed to be called.
Cheers.
As you might have already about knew the error. This is due to trying to access the empty array or trying to access the value of empty key of array. In my project, I am dealing with this error with counting the array and displaying result.
You can do it like this:
if(count($votes) == '0'){
echo 'Sorry, no votes are available at the moment.';
}
else{
//do the stuff with votes
}
count($votes) counts the $votes array. If it is equal to zero (0), you can display your custom message or redirect to certain page else you can do stuff with $votes. In this way you can remove the Notice: Undefined offset: 0 in notice in PHP.
If you leave out the brackets then PHP will assign the keys by default.
Try this:
$votes = $row['votes_up'];
$votes = $row['votes_down'];
In my case it was a simple type
$_SESSION['role' == 'ge']
I was missing the correct closing bracket
$_SESSION['role'] == 'ge'
If you are using dompdf/dompdf and error occure in vendor/dompdf/dompdf/src/Cellmap.php then
It looks like we're using the wrong frame id in the update_row_group method. Initial testing seems to confirm this. Though that may be because this is strictly a paged table issue and not too many of the documents in my test bed have paged tables.
Can you try changing line 800 to:
$r_rows = $this->_frames[$g_key]["rows"];
($g_key instead of $r_key)
https://github.com/dompdf/dompdf/issues/1295
Use mysql row instead
mysql_fetch_row($r)
Meanwhile consider using mysqli or PDO
?>
Try seeding data with command: php artisan db:seed.
its just a warning use:
error_reporting(0);
it shows when we do not initialize array and direct assigning value to indexes.
somefunction{
$raja[0]="this";
$raja[1]="that";
}
instead :
somefunction{
$raja=array(0=>'this',1='that');
//or
$raja=array("this","that");
}
it just notification, do not generate any output error or any unexpected output.
I have a php variable: $foo
My MySQL table called data has the following structure:
id var header
1 zj3 http://google.com
I would like to check if $foo is all ready in var row.
If it is I would like to echo header ("http://google.com")
How would you approach this?
Thanks in advance, please ask if any clarification is needed!
Your query should be:
SELECT `header` FROM `data` WHERE `var` = '$foo'
This will return all the headers with a var value of $foo.
$db = mysqli_connect('localhost', 'username', 'password', 'database');
if($query = mysqli_query($db, "SELECT `header` FROM `data` WHERE `var` = '$foo'")){
while($row = mysqli_fetch_assoc($query)){
echo $row['header'];
}
mysqli_free_result($query);
}
first connect to the db
$query = mysql_query("SELECT var, header FROM data WHERE id='1'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)){
if($foo == $row['var']){
echo $row['header'];
}
}
EDIT: changed equality statement based on your edit
It's not difficult at all, If I understand correctly then this should help you.
// Query Variable / Contains you database query information
$results = $query;
// Loop through like so if the results are returned as an array
foreach($results as $result)
{
if(!$result['var'])
echo $result['header'];
}
// Loop through like so if the results are returned as an object
foreach($results as $result)
{
if(!$result->var)
echo $result->header;
}
are you asking if $foo matches any of the fields in data, or if $foo=some_field? Here for if you want $foo==var.
$foo='somevalue';
$query="SELECT id, var, header FROM `data` WHERE var='$foo'";
$result=mysqli_query($query);
if($result->num_rows==0)
$loc= 'http://google.com';//default value for when there is no row that matches $foo
}else{
$row=$result->fetch_assoc(); //more than one row is useless since the first header('Location: x') command sends the browser to a new page and away from your script.
$loc=$row['header'];
}
header ("Location: $loc);
exit;
ETA: since you've edited your question, it appears that you want to echo the header column if your search value matches your var column. The above won't work for that.
You just want to know if $var's value is anywhere in that column (any row(s))?
SELECT COUNT(id) FROM data WHERE var = ?;
The result will be the number of rows for which the field var contains the value of $var.
Here's a template for all the "does it exist" questions.
This is the only thing that actually worked for me so far and is not deprecated.
if ($query = mysqli_query($link, "SELECT header FROM data WHERE var = '$foo'")) {
$header = mysqli_fetch_assoc($query);
if ($header) {
// The variable with value $foo exists.
}
else {
// The variable with value $foo doesn't exist.
}
}
else {
// The query didn't execute for some reason. (Dammit Obama!)
}
WARNING!
Even if the variable DOES NOT EXIST the comparison between $query and mysqli_query() will always return TRUE.
The only way --which happened to me-- for the comparison to return FALSE is because of a syntax error in your query.
I don't know why it worked for the guy who wrote the accepted answer, maybe it's an update or maybe he had a syntax error and was so confident that he didn't check if it could ever be TRUE.
Here's the comment someone made for correcting his syntax:
"Add another ) before the { in the first line"
So, the accepted answer is WRONG!