-
Posts
322 -
Joined
-
Last visited
Everything posted by CyberShot
-
I have googled this for a while and I am getting lots of different results. Is there a standard method used for checking to see if a value exists in a database before inserting a value? I have a form, you type your name and push submit, it inserts the name into the database and then the name shows up in a list. Right now, it accepts the same values. I want to add a check to say if name exists, do something
-
my code works. But when I remove this if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } include('connection.php'); $pattern = '/[A-Za-z]+/'; if(isset($_POST['name']) && preg_match($pattern, $_POST['name'])) { $name = $_POST['name']; $sql="INSERT INTO mytable (names) VALUES ('$name')"; header("Location: http://localhost/php_sandbox/dbtest/index.php"); } else { echo "You must only use letters a to z"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); the code stops working. Why would it stop working?
-
are the single quotes in [code $_POST['name'] optional? The code works without them.
-
I am trying to use preg_match to validate input. I don't know why but when I push the submit button, instead of inserting the correct value, It inserts a 1. I have a simple form that you type your name and push submit. When you do that, the code puts the name in a list above the form. There is a drop down menu that you can select the name from a list and push submit to delete the name. It all works. I tried to use preg_match to validate the name being input and I get a one every time I submit. I don't know why. This is all the code $pattern = '/[A-Za-z]+/'; $name = preg_match($pattern, $_POST[name]); if(isset($_POST[name])) { $sql="INSERT INTO mytable (names) VALUES ('$name')"; header("Location: http://localhost/php_sandbox/dbtest/index.php"); }
-
I see, that makes sense. Well then if I throw in another check for the query to only perform if the post is set then it should work. That is my code posted above. I have all the code split up into several files, so each part is very small. There is insert, delete, connection, and index. The insert code has been posted
-
You lost me. The sql is defined in the above insert statement. To insert the name into the database. then if if works, it shows the name in the list. The code worked fine until I added the check to see if the name field was empty. So I don't understand why it would error out now. Thanks for your help by the way.
-
I just cought on to that. But now it creates a new problem, the code errors out on the sql check. if(isset($_POST['name']) && !empty($_POST['name'])){ $sql="INSERT INTO mytable (names) VALUES ('$_POST[name]')"; } else { echo "you must put in a name"; } if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } now, if I just push the submit button, it gives me the echo error, but it also says Undefined variable: sql the mysql_query is not part of the else statement, so why would it now error out?
-
I keep getting an error from that Parse error: parse error, expecting `','' or `')'' in C:\wamp\www\dbtest\insert.php on line 5 if(isset($_POST['name'] && !empty($_POST['name'])){ $sql="INSERT INTO mytable (names) VALUES ('$_POST[name]')"; } else { echo "you must put in a name"; }
-
I have a form that i can enter a name and it inserts the name into a database. I am trying to add validation now. right now, if i push the submit button, it puts in an empty value into the list. So I tried two things if(isset($_POST[name])){ insert name into database } else { echo "you must put in a name" } I also tried [code] if(!isset($_POST[name])) return false; [code] neither worked. It still inserts a null value. What am I doing wrong?
-
[SOLVED] help with removing data from table
CyberShot replied to CyberShot's topic in PHP Coding Help
got it figured out. thanks for the help guys -
pull database info depending on form variables
CyberShot replied to pandaweb's topic in PHP Coding Help
I don't know a whole lot but I just got something like this working. I had a drop down list that would get the values of my table. You need a loop to get them, an array to store them and another loop to display them. so get the info like this $mysql = new mysqli('localhost', 'root', 'your db pass', 'your database') or die('not working'); $result = $mysql->query("SELECT * FROM your table") or die($mysql->error); if($result) { $name = array(); while($row = $result->fetch_object()) { $name[] = $row->names; } foreach($name as $n): echo $n . "<br />"; endforeach; } so the above code will echo out the results of the name field on name per line. Using the code below, it will populate a drop down list with the same names <form action="remove.php" method="post"> <select name="dropname"> <?php foreach ($name as $n): ?> <option value="<?php echo $n; ?>"><?php echo $n ?></option> <?php endforeach; ?> </select> <input type="submit" name="go" value="delete name" /> </form> to get the values from the form, you set the method to post then you can get them this way on the button for the form, you give it a name. <input type="submit" name="submit" value="submit name" /> the name field is the name of the variable for php, then use the post array to get the value $name = $_POST[name]; for my purposes, I wanted the dropdown list to be populated with the names I submit in the form using the code above, then if I chose a name in the drop down list, it will delete the name from the database using this code $result = mysql_query("DELETE FROM mytable WHERE names = '$_POST[dropname]'"); notice the use of $_post[dropname] dropname is the variable I am using the drop down list. Look above on the select tag, you can see name="dropname" -
[SOLVED] help with removing data from table
CyberShot replied to CyberShot's topic in PHP Coding Help
well I found one booboo I changed the code to this if(isset($_POST['go'])) { $remove = $_POST['drop_name']; mysql_query("DELETE FROM mytable WHERE names = '$remove'"); error_reporting(E_ALL); } but still no joy -
[SOLVED] help with removing data from table
CyberShot replied to CyberShot's topic in PHP Coding Help
I do have more than one name in the names column. it is a first and last name. But I did not make two colums for the names. I did add the error code like this but get nothing $remove = $_POST['drop_name']; DELETE FROM mytable WHERE names = '$remove'; error_reporting(E_ALL); -
[SOLVED] help with removing data from table
CyberShot replied to CyberShot's topic in PHP Coding Help
@PFMaBiSmAd I know answers can be found by reading documentation. I also know that you can write a book on how to read the documentation. I also know that if everyone went to the documentation, there would be no need for this forum. I was going through the documentation. Notice that I did't ask you how to do it, I asked help with code I have been trying @lemmin I tried the code you posted two seconds before receiving your reply, I get the same problem, the page blinks and nothing is removed. -
I have a form that I can type in a name and it displays the name in a list. Below that I have a drop down list that is populated with the same names. What I need is that when you select a name in the drop down list and press the button, it will remove the name from the list. As is right now, I don't get any errrors, but when the page refreshes, the name is still there. Here is what I tried if(isset($_POST['go'])) { $remove = $_POST['drop_name']; $sqlRemove ="DELETE FROM mytable(names) VALUES('$remove')"; } and the drop down list <form method="post"> <select name="drop_name"> <?php foreach ($name as $n): ?> <option><?php echo $n ?></option> <?php endforeach; ?> </select> <input type="submit" name="go" value="remove name" /> </form> my database name is data, table name is mytable and there is two fields.. id, names
-
i had that at one time. I had to change it to add the drop down
-
can you offer a better sollution? The code works this way. One loops returns the value for a drop down list, the other one returns the same value for a normal list. Just displays the names on the screen.
-
figured it out. I had to move the foreach loop out of the while loop and now it works. like this $mysql = new mysqli('localhost', 'root', 'pascal35', 'data') or die('not working'); $result = $mysql->query("SELECT * FROM myTable") or die($mysql->error); if($result) { $name = array(); while($row = $result->fetch_object()) { $name[] = $row->names; } foreach($name as $n): echo $n . "<br />"; endforeach; } is that propper coding for the foreach loop? I just copied the foreach loop that you posted above.
-
well, that worked for my drop down, but messed up the list. So I tried putting in the foreach loop and it kinda worked but is outputting to many names.
-
I have a test database set up on localhost. I have a form that I can type a name into, hit the button and it puts the name into the database and into a list on my page. it works fine. Now, I wanted to make a drop down list with all the names so that you could select one to remove that name from the database. So I did this <form> <select> <option><?php echo $name ?></option> </select> </form> What that did was list the very last name in the list. What I thought it would do was list all the names based on the loop that I have set up to read all the names from the database. which is this $mysql = new mysqli('localhost', 'myusername', 'mypass', 'mydatabase') or die('not working'); $result = $mysql->query("SELECT * FROM myTable") or die($mysql->error); if($result) { while($row = $result->fetch_object()) { $name = $row->names; echo $name . "<br />"; } } Now the while loop is in a file I called results and it is included in the index page with an include. I thought this would give me access to the $name variable and allow me to use it the same way it's being used in the loop. of course I was wrong, so how do I use it the same way?
-
when I first installed wamp, I could go to localhost and it would display a nice user friendly tool that had links to php myadmin and other things. Now when I go to localhost, all I get is a directory listing of the folders in the www directory. Does anyone know how to fix that? I don't know what changed
-
well, now it's working. Not sure what I did, but I am getting the page I need. Thanks for the help guys
-
well, I deleted that bit of code becuase it doesn't really need to be there. The submit buttons works now but I still can not get anything out of the php code. I tried just a simple one <?php if(isset($_POST['submit'])){ header('location: newpage.php'); } ?> it just throws a # sign onto the end of the address
-
I packaged up the whole thing. You can download it here http://www.photobyiris.com/login.zip I stripped out all the javascript and the code started working. as it is, the php don't work, but If I remove everything but the form code, the php starts working. Thanks for your help.
-
<fieldset id="signin_menu"> <span class="message">Please verify your account before continue</span> <form method="post" id="signin" action="handle_form.php"> <label for="username" name="username">Username or email</label> <input id="username" name="username" value="" title="username" class="required" tabindex="4" type="text"> </p> <p> <label for="password" name="password">Password</label> <input id="password" name="password" value="" title="password" class="required" tabindex="5" type="password"> </p> <p class="clear"></p> <a href="change_password.php" class="forgot" id="resend_password_link">Forgot your password?</a> <p class="remember"> <input id="submit" value="Sign in" tabindex="6" name="submit" type="submit"> <input id="cancel_submit" value="Cancel" tabindex="7" type="button"> </p> </form> </fieldset> that is all the code. I have the doctype in the header.php with an include in the top of the index. with the form. then the footer.php included also. I have the php code for the form in the handle_form.php a simple test was all I was trying to do to make sure it was working. Then I was going to try to learn how to do the rest of the validation as the days go on.