Jump to content

ovi_gm

New Members
  • Posts

    7
  • Joined

  • Last visited

ovi_gm's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Well, I researched today for hours and I couldn't find any solution to the problem. There are some solutions with an IF statement that seem to be very logical but they don't work. It just doesn't execute the if statement. if( isset($_POST) ) { //submit data to MySQL } By the way. Is there anything simple that I could write so that I know if an IF statement works or not? For example like a beep or something? Just to check if the code gets to a certain point or not...
  2. Shouldn't there be a line in the PHP code that tells it to run when the submit button is clicked? Something's missing. I have many pages on this site. If I click on another page and then come back to this page, there is an empty record added to the database. And it makes sense to be like this because there is a php code that gets executed every time the page is loaded. I'm really stuck here....
  3. Ok. Schema is very simple. It is a database with a single table named contacts. The table has 2 columns: a primary Key ID and a name. I simplified it for the purpose of this topic. When I access my bootstrap modal, I enter for example name "Jim" in the input box. Then I click Submit. Then I have a new record in the database with ID 1 (or whatever is incremented). Now, everytime I refresh my page and do nothing else, there is a new record in the database with the new incremented ID and the same name (Jim). My logic says that php code is running everytime I hit reload. Otherwise, I don't understand how these entries appear.
  4. I know what you mean. Still, it doesn't seem right to me. You should be able to somehow execute that php ONLY when the submit button is pressed. What if I don't care if there are 2 or more contacts with the same name? It could be many identical entries only with the ID being different.
  5. Thank you very much for your time to write this message. The fact is that this is how I did it first, with the code on the same page, but I got items added to the database every time I did a refresh which is not ok. This is my code (I am writing only what is essential). I modified it like there's a single column in the database table (named "name"). <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <!-- Bootstrap core CSS --> <link href="css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type"text/css" href="css/style.css"> <script src="http://cdn.ckeditor.com/4.6.1/standard/ckeditor.js"></script> </head> <body> <?php $name = $_POST["name"]; $servername = "localhost"; $username = "admin"; $password = "1234"; $dbname = "database"; $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); try { $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO contacts (name) VALUES ('$name')"; $conn->exec($sql); } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?> ------- HTML CONTENT ------------- ------- AND THEN THE MODAL ------- <div class="modal fade" id="addContact" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <form action = "contact.php" method="post"> <!-- .....content with all the inputs --> <input type="text" name = "name" value="" class="form-control" placeholder="Name of contact..."> <button type="submit" class="btn btn-primary">Save to database</button> </form> </div> I tried to put an if statement for the php code: if (isset($_POST['submit'])) { and here were the SQL statements } I even tried to set the name to null after the php code and then, at page refresh, check if the name is null or not. It doesn't work. It inserts the same record into the database everytime a do a refresh of the page. I am very good at Visual Basic programming and I think the philosophy of php is a bit different. There are some logic things that I don't get yet. Pls help.
  6. Hello everyone, I am new to php and I need a confirmation if this is the correct approach for a code I wrote. Basically, I have a contacts.php page where I have a bootstrap table and a modal with some fields to add a new contact. I managed to write all the code, it works perfect but I want to know if this approach is ok. The application will be much more complex and I don;t want to start on the wrong foot here. I have 2 files: contacts.php and add_new_contact.php. First file, contacts.php: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <!-- ............ --> </head> <body> <!-- ............ here is the page layout--> <!-- Then I have my modal from bootstrap --> <div class="modal fade" id="addContact" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <form action = "add_new_contact.php" method="post"> <!-- .....content with all the inputs --> <button type="submit" class="btn btn-primary">Save to database</button> </form> </div> </body> </html> Now, in form action I am telling html to go to add_new_contact.php where I wrote the code for inserting all the values to the database. When doing this, it opens the page and it stays blank because there is no html there. So, in that php file I added a redirect code to the initial contacts.php. Here is the code: Second file: add_new_contact.php: <?php //all the code needed to insert the contact in the database header("Location: http//..../contacts.php"); exit(); ?> So, this works fine. But is this the best way to do it? Thank you.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.