• Resolved hsysgrp

    (@hsysgrp)


    Am a novice, Attempting to insert data into a custom table.

    <?php
    			$link = mysqli_connect("localhost", "h8888", "88888", "h8888"); 
    			
    			if($link === false){    
    			die("ERROR: Could not connect. " . mysqli_connect_error());
    			} 
    			$ID = $_POST('ID');
    			$FirstName = $_POST['FirstName'];
    			$Lastname = $_POST('Lastname');
    			$Address1 = $_POST('Address1');
    			$SQL = "INSERT INTO AAUW_MEMBERS(ID,FirstName,Lastname,Address1) VALUES ('$ID','$FirstName','$Lastname',$Address1')";
    			if(!mysql_query($link,$SQL))
    				(
    					echo "Not Inserted';
    			)
    			else
    				(
    					echo 'Inserted';
    				)
    			get header();
    ?>

    This is a test site.
    insert.php is in the child folder, under functions.php, this connection has been tested.
    <form method="post",action="insert.php">
    when i click save, nothing happens, no error messages,debug in config.php is set to ‘true’, no data appears in the table in phpMyAdmin no error in finding the file.
    Thank you for any help.
    Claudette

    • This topic was modified 4 years, 6 months ago by bcworkz. Reason: code fixed

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • May I know the reason you are creating custom table and why you want to push the data there this way? This can be done in a different way using set of plugins.

    Thread Starter hsysgrp

    (@hsysgrp)

    The page is https://hsysgrp.com/new-member-test/. I know I can use plugins, I am learning, and do not know what is wrong, and also why no error messages.

    Moderator bcworkz

    (@bcworkz)

    Your form’s action value should be a full URL to your file. Don’t try to use relative paths in WP, there will be situations where they fail, no matter how well considered.

    Your file does not utilize WP resources, so the WP_DEBUG definition has no bearing. Submitting the form should result in a 404 response. If you don’t get even that, there’s something wrong with your form.

    I don’t know offhand if it matters, but you don’t need commas between HTML tag attributes. More like this:
    <form method="post" action="https://localhost/wp-content/themes/my-theme/insert.php">

    BTW, when you post code in these forums, please use the code button or demarcate using backticks. When you don’t, the forum’s parser corrupts your code. I fixed you OP code for you.

    Thread Starter hsysgrp

    (@hsysgrp)

    Thank you for your response. My URL according to Bluehost doc guidance is
    <form method=”post” action=”https://hsysgrp.com/wp-content/themes/twentytwelve-child/insert.php”&gt;
    ID: <input type=”text” name=”ID”>
    FirstName: <input type=”text” name=”FirstName”>
    LastName: <input type=”text” name=”LastName”>
    Address1: <input type=”text” name=”Address1″>
    <input type=”submit”>
    </form>That elicited
    This page isn’t working hsysgrp.com is currently unable to handle this request.
    HTTP ERROR 500

    Moderator bcworkz

    (@bcworkz)

    There’s an error on insert.php somewhere. Check you error log for clues or check the code with something like https://phpcodechecker.com/

    Thread Starter hsysgrp

    (@hsysgrp)

    Making progress; php no errors now, thank you for the codechecker but got
    Connected successfully, Error, no record inserted.

    <?php
    			$link = mysqli_connect("localhost", "hsys99999", "De99999", "h99999"); 
    			
    			if(!$link) {    
    			die ("ERROR: Could not connect. " . mysqli_connect_error() );
    			} 
    			echo "Connected successfully";
    			$ID = $_POST['ID'];
    			$FirstName = $_POST['FirstName'];
    			$Lastname = $_POST['Lastname'];
    			$Address1 = $_POST['Address1'];
    			$SQL = "INSERT INTO AAUW_MEMBERS(ID,FirstName,Lastname,Address1) VALUES ('$ID','$FirstName','$Lastname','$Address1')";
    			
    			if(mysqli_query($link,$SQL)) {
    					echo " New Record Inserted" ;
    			} else
    			{
    					echo "Error: No record inserted" ;
    			}
    			mysqli_close($link) ;
    ?>
    • This reply was modified 4 years, 6 months ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    Add print_r( $_POST ); to ensure the forms values are transmitted correctly. I’ve no reason to believe they wouldn’t be, but lets make sure.

    Get into the phpMyAdmin app and navigate to the database containing the table. Click on the SQL tab and execute this:
    INSERT INTO AAUW_MEMBERS(ID,FirstName,Lastname,Address1) VALUES ('1234','Foo','Bar','123 sample st, city')
    Edit this query’s values as needed for your data types before execution. The app should give you a clue to the problem if insertion fails. If insertion works, check the DB permissions for the DB user your PHP code uses. It’s apparently restricted in some way.

    Thread Starter hsysgrp

    (@hsysgrp)

    Manual Insert in phpMyAdmin works fine.
    Added the print_r statements and found Firstname != FirstName
    DB user has all privileges like Admin`

    Connected successfully 12 Jane 4 Lecco Lane Error: No record inserted

    No errors from codechecker

    <?php
    			$link = mysqli_connect("localhost", "hsysgrpc_99999", "Desero999!", "hsysgrp999999"); 
    			
    			if(!$link) {    
    			die ("ERROR: Could not connect. " . mysqli_connect_error() );
    			} 
    			echo "Connected successfully";
    			$ID = $_POST['ID'];
    			print_r($ID);
    			$FirstName = $_POST['FirstName'];
    			print_r($FirstName);
    			$Lastname = $_POST['LastName'];
    			print_r($LastName);
    			$Address1 = $_POST['Address1'];
    			print_r($Address1);
    			$SQL = "INSERT INTO AAUW_MEMBERS(ID,FirstName,LastName,Address1) VALUES ('$ID','$FirstName','$LastName','$Address1')";
    			
    			if(mysqli_query($link,$SQL)) {
    					echo " New Record Inserted" ;
    			} else
    			{
    					echo "Error: No record inserted" ;
    			}
    			mysqli_close($link) ;
    ?>

    So close.

    Thread Starter hsysgrp

    (@hsysgrp)

    Success. Turns out a http 500 error just means the table is AAUW_Members instead of AAUW_MEMBERS. Works fine, thanks for all the help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Failed Insert’ is closed to new replies.