Check duplicates name ignore entry - php

I am using following code to insert entry in my database. But i can add same entry multiple time and i wanna that whenever i submit same entry in input type then message will show "Nickname already exists "
<div>
<form>
<div>name <input type="text" name="na"/></div>
<div>marks1 <input type="text" name="m1"/></div>
<div>marks2<input type="text" name="m2"/></div>
<div>marks3 <input type="text" name="m3"/></div>
<div> <input type="submit" name="save" value="save"/></div>
</form>
</div>
And Here is my php code:-
<?php
if(!empty($_GET['save'])){
$na=$_GET['na'];
$m1=$_GET['m1'];
$m2=$_GET['m2'];
$m3=$_GET['m3'];
$query="insert into student(name,marks1,marks2,marks3) values('$na','$m1','$m2','$m3')";
mysqli_query($connect,$query);
}
?>

Make a unique column in database for example unique name for every student. So when you enter new record first check it that either it is present in the database or not. If its there then you can show error to user
For example you can set a unique constraint like this

Do a select before inserting in your base to check if the username already exists.
For your input message showing that the username already exists you can use a hidden paragraph:<p id="informationText" hidden>This paragraph should be hidden.</p>
Then once your select request has returned that the tuple already exists do an ajax query to show up this paragraph: $('#informationText').removeAttr('hidden');
That's a simple way, there is other better way to do that you can do some research about JavaScript Form Validation
FIRST EDIT
Here is a code sample:
<?php
if( isset( $_GET['save'] ) ){
$na=$_GET['na'];
$m1=$_GET['m1'];
$m2=$_GET['m2'];
$m3=$_GET['m3'];
$query = "select * from student where name = ? ";
$result = mysqli_query( $connect,$query, array( &$na ) );
$row = mysqli_fetch_row( $result );
if (count($row) > 0) {
echo "<script>$('#informationText').text = "Username already exists";$('#informationText').removeAttr('hidden');</script>";
} else {
$query="insert into student(name,marks1,marks2,marks3) values('$na','$m1','$m2','$m3')";
mysqli_query($connect,$query);
}
}
?>

Related

While loop - Only one input from many others is sending a value through POST

This is the code where I get my input names and values from a table called optionale and doing something with these:
<form role="form" autocomplete="off" action="includes/functions/fisa-init.php" method="POST">
<?php
connectDB();
$query = mysqli_query($mysqli, "SELECT * FROM `optionale`") or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($query))
{
?>
<span><?php echo $row['denumire']; ?></span>
<input type="text" name="nrBucati">
<input type="hidden" value="<?php echo $row['cod']; ?>" name="codProdus">
<?php } ?>
</form>
The optionale table looks like this:
The HTML looks like this:
As you can see in the last picture, I have in HTML, a name for input (taken from optionale table) and the actual input in which I write a value.
fisa-init.php:
$stmt3 = $mysqli->prepare("
UPDATE
`stocuri`
SET
`cantitate` = `cantitate` - ?
WHERE `cod` = ?
");
$stmt3->bind_param("is", $bucata, $cod);
// set parameters and execute
$bucata = mysqli_real_escape_string($mysqli, $_POST['nrBucati']);
$cod = mysqli_real_escape_string($mysqli, $_POST['codProdus']);
if (!$stmt3->execute())
{
echo "Execuția a întâmpinat o eroare: (" . $stmt3->errno . ") " . $stmt3->error;
}
$stmt3->close();
$mysqli->close();
In the code above (fisa-init.php) I am trying to take all the input values from my HTML and update rows in another table called stocuri:
As you can see, only the second row from stocuri table was updated, but I wrote values in all 5 inputs. It got only the last input.
How to modify the while loop in order to take all my inputs value?
If something is not clear, I apologize a hundred times. I will explain all the informations that are needed.
P.S. cod in table optionale is the same with cod in table stocuri. In this way, I know where to update values.
Each <input> MUST have an individual name or a named array.
So give each an aditional number like name1,name2 or use an named array like name[]
Finally this name="codProdus[]" is your solution.
Read more here
HTML input arrays
Have a nice day

How to update Grid on PHP page as per data from MySQL

I have MySQL table which is presently displayed using a grid-table on the PHP page. I want the data to be updated:
Where a particular field of a record displayed on PHP is changed.
Whenever a new record is added to the table.
Delete grid-table row when the record is deleted in the MySQL Table.
Step by step method to Update a field:
//First the user will specify what field to Update
<form method="post">
<p>Field to Update:
<input type="text" name="pupdate"></p>
//Next the user will update the field
<p>Updated Name:
<input type="text" name="pname"></p>
<p><input type="submit" value="Add New"/></p>
</form>
<?php
//To connect to database(If you dont understand the next line I can explain it further)
require_once "db.php";
if ( isset($_POST['pupdate']) && isset($_POST['pname']))
{
$up = $_POST['pupdate'];
$u = $_POST['pname'];
//This will update the database with the new field
$sql = "UPDATE NAMEOFTABLE
SET pname='$u'
WHERE pname='$up'";
echo "<pre>\n$sql\n</pre>\n";
mysql_query($sql);
}
?>

Error when trying to insert a comment on a blog post php

I am trying to insert comments on a blog post and I keep getting an error message. I don't know if the database is set correctly, or if i'm missing some syntax error. I have a form page and a handle page. This is the form:
<h1><center> Add comment form</center></h1>
<form action="hc.php" method="post">
<fieldset>
<h3>Post comment.</h3> <br>
<textarea name="comment"cols="50"rows="10"id="comment" >
</textarea><br>
<input type="hidden" name="comid" value="'.$comid .'"/>
<input type="hidden" name="blogid" value="'.$blogid .'"/>
<input type="submit"value="Submit"name="submit" />
<input type="reset"value="Clear">
</fieldset>
</form>
My handle page is as following:
<?php
include ('./includes/mysqli_connect.php');
$blogid = $_SESSION['blogid'];
$comment = $_SESSION['comment'];
$comdate = $_SESSION['comdate'];
$comid = $_SESSION['comid'];
$userid = $_SESSION['userid'];
//if (isset($_GET['blogid']) && isset($_GET['userid']) && isset($_GET['comment'])){
$query = "INSERT INTO comments(comid, blogid, userid, comment, comdate) VALUES ('$comid', '$blogid','$userid','$comment','$comdate')";
$result= #mysqli_query($dbc,$query);
$comid=NULL;
$blogid=NULL;
$userid=NULL;
if ($results) {
echo "Thank you your information has been submitted.";
} else {
echo "There was an error! " . mysqli_error($dbc);
}
?>
I am new at php and appreciate the help.
The error message is:
There was an error! Cannot add or update a child row: a foreign key
constraint fails (codecrew_users.comments, CONSTRAINT
comments_ibfk_1 FOREIGN KEY (blogid) REFERENCES blog (blogid))
$query = "INSERT INTO comments(comid, blogid, userid, comment, comdate) VALUES ('$comid', '$blogid','$userid','$comment','$comdate')";
So... Your table comments has some field on it that requires a foreign key.
I'm not sure which value is causing the error however one of your values does not match an id in another table.
So one of these has an invalid value:
'$comid', '$blogid','$userid','$comment','$comdate'

Update MySQL entries via PHP/Html & sql

Im trying to create a form where based on someones first and surname, their email can be changed.
So the html looks like this:
<form action="sUpdateResponse.php" method="post">
<input type="text" placeholder="Enter Email..." name="sUpdateEmail">
Where the name is
<input type="text" placeholder="Enter Forename..." name="sUpdateFN">
<input type="text" placeholder="Enter Surname..." name="sUpdateSN">
<input type="submit" value="Update Records" name="sRetrieveUpdate"></form>
This takes a new email to update the data entry where the forename and surname exist.
The php on sUpdateResponse looks like this,
if($_POST['sRetrieveUpdate'])
$queryRetrieve = mysql_query( "UPDATE staffData SET sEmail='".$_POST['sUpdateEmail']."' WHERE sFN='".$_POST['sUpdateFN']."'
AND sFN='".$_POST['sUpdateSN']."'" );
This doesn't return an error but doesn't seem to alter the email either...
Where am i going wrong?
<?php
if(isset($_POST['sRetrieveUpdate'])){
if(isset($_POST['sUpdateEmail']) && isset($_POST['sUpdateFN']) && isset($_POST['sUpdateSN'])){
$query = "UPDATE staffData SET sEmail = '.$_POST['sUpdateEmail'].' WHERE sFirstName = '.$_POST['sUpdateFN'].' AND sSurName = '.$_POST['sUpdateSN']";
$Result = mysqli_query($query);
}else{
// Error Message
}
}else{
// Error Message
}
?>
"UPDATE staffData SET sEmail='".$_POST['sUpdateEmail']."' WHERE sFN='".$_POST['sUpdateFN'].$_POST['sUpdateSN']."'"
Your Second column is same in where condition sFn repeated.
WHERE sFN='".$_POST['sUpdateFN']."'
AND sFN='".$_POST['sUpdateSN']."'")
It cheks two values in same column . There is your column name mistake in the query.make it correct then it will work fine :)
It should be Something like this
if($_POST['sRetrieveUpdate'])
$queryRetrieve = mysql_query( "UPDATE staffData SET Email='".$_POST['sUpdateEmail']."' WHERE sFN='".$_POST['sUpdateFN']."' AND sSN='".$_POST['sUpdateSN']."'" );

Selecting ID in while statement PHP

I'm attempting to make a function that will allow users to delete posts they have left. Basically I'm trying to select an ID and then pass it off to another function to delete it. The current code is deleting every comment, not just the comment linked to the ID like I'm attempting to.
Here are the table values
Table eventUpdates
ID - unique ID. Trying to use this value to delete the post
eventID - references an eventID from another table
eventReply - the "message" or response
eventUserID -ID of the user posting the message
username -username of the person
eventTimestamp -timestamp
So, currently every response is being deleted. I can't get it to delete just the ID that I send.
<table border ="1">
<?
$eventUpdates = mysql_query($sql);
while ($list = mysql_fetch_assoc($eventUpdates)) {
echo $updateID;
echo '<tr><td>';
echo $list['username'],"</br>", $list['eventTimestamp'],'<br/>';
if($list['eventUserID'] == $userid){ //used for deleting posts. Checks the session to make sure it's the user who made the post
?>
<form method="post">
<input type="submit" name="deleteUpdate" value="Delete Update">
</form>
<?
if(isset($_POST['deleteUpdate'])){
$updateID = $list['ID'];
$delete = new postprocessing;
$delete->deleteEventUpdate($updateID);
echo '<meta http-equiv="refresh" content="0;url=main.php">';
}
}
echo '</td><td>';
echo $list['eventReply'];
echo '</td></tr>';
}
Here is the function used to delete it
function deleteEventUpdate($ID){
mysql_query("delete from eventUpdates where ID = '$ID'");
}
This is because you haven't specified the ID you want to delete in the post array, so every comment they created "matches" the deletion criteria. The $_POST array looks like:
$_POST = array('deleteUpdate'='Delete Update');
so when you're looping through the list of events, every comment that the user 'owns' ($list['eventUserID'] == $userid) matches the criteria "isset($_POST['deleteUpdate'])":
You need to include a hidden input with the ID the specific comment you want to delete and match against it as well:
<form method="post">
<input type="submit" name="deleteUpdate" value="Delete Update">
<input name='post_id' value="<?php echo $list['ID'];?>" type='hidden' />
</form>
and then in the loop:
if(isset($_POST['deleteUpdate']) && isset($_POST['post_id']) && $_POST['post_id']==$list['ID']){...

Categories