Jump to content

Database issues and working


heeha

Recommended Posts

<?php 
$servername="localhost";
$username="u992092914_trial"
$password="qgOq5Sy6Us";
$dbname="u992092914_trial";
 
$myconn = new mysqli($servername, $username, $password, $dbname);
 
//check  connection 
 
if($myconn->connect_error){
echo "(connection failed." .$myconn->connect_error);

else {
 
echo "connected succesfully";
}
 
//create table;
 
$user_query= "CREATE TABLE u992092914_trial(
id HOST AUTO_INCREMENT PRIMARY KEY,
 class VARCHAR (50), 
 TTL VARCHAR (50) , 
 TYPE VARCHAR (50), 
 IP_ADDRESS VARCHAR (50), 
 RNAME VARCHAR (50), 
 MNAME VARCHAR (50), 
 REFRESH VARCHAR (50), 
 RETRY VARCHAR (50),
 EXPIRE VARCHAR (50),
 MIN-TTL VARCHAR (50),
 TXT  VARCHAR (50))";
 //while getting the dns records for any website there would be more than one records such as ttl would be more than 3-4 times, how do i put them so that each records get collected?
VALUES( )//how do i get this value from form itself?
 
if($myconn->query($user_query)===TRUE){
echo "Tables created successfully";
} else {
echo " tables cant be created." .$myconn->error);
}
$myconn-close();
?>
 
<form action="<?php echo $_SERVER["PHP_SELF"];?> method="post">
Enter url <input tpye="text" name="url">
submit<input type="submit">
</form>

 ouput would be dns records such as : 

 Host Class TTL Type IP Address

 example.com IN 4 A 00.000.216.34

 

 Host Class TTL Type IP Address

 example.com IN 4 A 124.000.216.34

 

 Host Class TTL Priority Target

 example.com IN 4 NS    my-servers.net

 

 . How to process them that they entered directly into databases and after which i can output them from database to my page. 

 

 what did i do wrong at creating code processing that i m seeing this error.. 

 

 Parse error: syntax error, unexpected '$password' (T_VARIABLE) in /home/u992092914/public_html/b/mysql.php on line 4

Link to comment
Share on other sites

Besides your missing semicolon on line 3 that requinix pointed out...

 

 

//while getting the dns records for any website there would be more than one records such as ttl would be more than 3-4 times, how do i put them so that each records get collected?
VALUES( )//how do i get this value from form itself?

 

The values are not coming from a form, only the url value is.

That url value would be used to connect to the url with something like curl to obtain this data

If you expect multiple values in a field, you should have an additional table and linking to that columns id.

 

Forget php_self even exists, is insecure. You can leave the action empty if is going to the same script. Plus you forgot a double quote originally.

<form action="" method="post">

 

Another error this line.

<input type="text" name="url">

Link to comment
Share on other sites

Sorry that there were lot of syntax error, I have corrected them and now able to create table but rest of the queries still needs to be solved.

<?php 
$servername="localhost";
$username="u992092914_trial";
$password="qgOq5Sy6Us";
$dbname="u992092914_trial";

$myconn = new mysqli($servername, $username, $password, $dbname);

//check  connection 

if($myconn->connect_error){
echo ("connection failed." .$myconn->connect_error);
} 
else {

echo "connected succesfully";
}

//create table;

$user_query= "CREATE TABLE u992092914_trial(
id INT(20) AUTO_INCREMENT PRIMARY KEY,
host VARCHAR(50),
class VARCHAR(50), 
TTL VARCHAR(50), 
TYPE VARCHAR(50), 
IP_ADDRESS VARCHAR(50), 
RNAME VARCHAR(50), 
MNAME VARCHAR(50), 
REFRESH VARCHAR(50), 
RETRY VARCHAR(50),
EXPIRE VARCHAR(50),
MIN_TTL VARCHAR(50),
TXT VARCHAR(50))";
 /*while getting the dns records for any website there would be more than one records such as ttl would be more than 3-4 times, how do i put them so that each records get collected? */
//how do i get "value" from form itself to automatically gets added to dbase? */

if($myconn->query($user_query)===TRUE){
echo "Tables created successfully";
} else {
echo "tables cant be created." .$myconn->error;
}
$myconn->close();
?>

<form action="<?php echo $_SERVER["PHP_SELF"];?> method="post">
Enter url <input tpye="text" name="url">
submit<input type="submit">
</form>

<?php

 /* ouput would be dns records such as : 
 Host	Class	TTL	Type	IP Address
 example.com	IN	4	A	00.000.216.34
 
 Host	Class	TTL	Type	IP Address
 example.com	IN	4	A	124.000.216.34
 
 Host	Class	TTL	Priority	Target
 example.com	IN	4	NS	    my-servers.net
 
 . How to process them from form that they entered directly into databases and after which i can output them from database to my page. 
 
 */
 ?>
Link to comment
Share on other sites

Still need to fix your form.

 

Are you asking how to create a more advanced form and then retrieve the post values into your create statement?

 

Create the table(which you should check if table exists or not first)

perform a query using INSERT in a statement to add them

perform a query with a SELECT to fetch them.

Link to comment
Share on other sites

:Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /home/u992092914/public_html/b/mysql.php on line 60

<?php

$url= $_POST['url'];

if(isset($_POST['url']))  //did to check if url is there or not.
{
if(filter_input(INPUT_POST, 'url', FILTER_SANITIZE_URL)){
$url= trim($_POST['url']);
if(substr($url, 0, 7) !='http://') 
{
$url='http://' . $url;
}

if(filter_var($url, FILTER_VALIDATE_URL)) //did to check if your is valid or not
{

$host= str_ireplace('www.','',parse_url($url, PHP_URL_HOST));

$result= dns_get_record($host, DNS_ANY);

}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="Post">
Enter URL : <input type="text" name="url">

Submit: <input type="submit" name="submit">
</form>

<?php 
if(isset($result)){

$result =dns_get_record($host, DNS_ANY);//currently I have shown this so that i would get all info in array, but how will they be inserted into db, and will be fetched in a way to get information on webpage as they are in array? or DNS_A or DNS_MX putting them individually how do i create table so that i can fetch all the info from dbbase for a particular website at once?
foreach($result as $dnsinfo);
print_r($dnsinfo);//just to make sure that everything working
}
?>
<br>

<?php 
$servername="localhost";
$username="u992092914_trial";
$password="qgOq5Sy6Us";
$dbname="u992092914_trial";

$myconn = new mysqli($servername, $username, $password, $dbname);

//check  connection 

if($myconn->connect_error){
echo ("connection failed." .$myconn->connect_error);
} 
else {

echo "connected succesfully";
}

//insert data into tables
if(isset(u992092914_trial))// how to i check it exists or not, i mean did put it as variable but variable $user_query is same for inserting and for creating tables
{

$user_query= "INSERT INTO u992092914_trial( host, class, ttl, type, ip address, rname, mname, refresh, retry, expire, min ttl, txt), 
VALUES()"; // this should be filled itself when users put a query inside the form and sets data according to type?

 /*while getting the dns records for any website there would be more than one records such as ttl would be more than 3-4 times, how do i put them so that each records get collected? */
//how do i get "value" from form itself to automatically gets added to dbase? */

if($myconn->query($user_query)===TRUE){
echo "Tables created successfully";
} else {
echo "tables cant be created." .$myconn->error;
}
$myconn->close();
?>


<?php

 /* ouput would be dns records such as : 
 Host	Class	TTL	Type	IP Address
 example.com	IN	4	A	00.000.216.34
 
 Host	Class	TTL	Type	IP Address
 example.com	IN	4	A	124.000.216.34
 
 Host	Class	TTL	Priority	Target
 example.com	IN	4	NS	    my-servers.net
 
 . How to process them from form that they entered directly into databases and after which i can output them from database to my page. 
 
 */
 ?>
Link to comment
Share on other sites

in your previous thread about forms and form processing, the forum members were trying to get you to organize your code so that it grouped common actions together and that it put any form processing code before any code that gets or uses data to be displayed on the page. and now that you have added database code into the process, you need to put code that inserts/updates/deletes database data and code that retrieves data into the proper locations in the code on your page

 

the code on your page should be laid out in this general order - initialization, start of database dependent code, determine user state and permissions, post method form processing, get method business logic, end of database dependent code, get method presentation logic, and html page/template.

 

1) initialization - create/define things your code needs - session_start(), require files holding configuration data/function definitions, setup an autoloader for class definitions...

 

2) start of database dependent code - create a database connection. if you are using exceptions to handle database errors, this would be where the try block starts.

 

3) determine user state and permissions - check if the current user is logged in and retrieve any permissions the user has. the rest of the code on the page would make use of the logged in state and permissions to determine what code can be ran and what content will be produced.

 

4) post method form processing - the post method form processing code, which creates/modifies/deletes data on the server, should come near the start of your file so that you aren't tempted to output anything to the browser before any data has been updated by the processing code. if your page has multiple sections of form processing code, you would have them all groped together in this section of code.

 

after successfully (no errors) processing any post data, do a header() redirect to the exact same url that the form submitted to. this will cause a get request for your page. this will cause the browser to forget that a form was submitted and it won't try to resubmit the form data if you refresh the page or browse back to the same url. this also enforces separation of concerns. post method form processing, which modifies data on the server is a separate concern from displaying data due to a get request for your page. if you want to display a one-time 'success' message after the header() redirect, pass it in a session variable, then clear he session variable after the the message gets displayed.

 

if there are errors while processing any post data, you would not redirect, stay on the page, let the rest of the code on the page display the errors, (re)display the form, and repopulate the form fields with the previously submitted values.

 

5) get method business logic - code that produces/gets data needed for the dynamic content on the page. this code contains any database specific code that knows how to retrieve data from your database tables. the result of this code should be php variables that the code later on the page uses as its input. this code should contain NO html/css/javascript markup.

 

6) end of database dependent code - if you are using exceptions to handle database errors, you would catch the errors at this point. you can also destroy any query result resources and the database connection at this point since you won't need them any longer.

 

7) get method presentation logic - code that knows how to take the data (database data, errors, form data...) from ALL the above code and produce the dynamic output for the page. if the output doesn't require any 'heavy' processing/formatting, just use the data directly in the html page/template code. the result from this code should be php variables that the html page/template uses. this code should contain NO database specific statements. if your page has multiple sections of get method presentation logic, you would have them all groped together in this section of code.

 

8) html page/template - this section starts with the <!DOCTYPE ... tag and ends with the </html> tag. it is the actual html document that the dynamic output is put into to make the complete page. only simple php conditional logic/loops, function calls (that are responsible for producing output), and echo statements should be present in this section of code.

 

if you organize the code on your page like this, it will separate all the different concerns, making it easier to see what your code is doing, easier to test, and easier to get help with because you can isolate and post just the relevant part. also, in your 'Is php easy' thread, i made a post about defining the inputs, processing, and output/result for your code. if you do this for your form processing code, it will make it easier for you to write code that does what you want.

Link to comment
Share on other sites

I will the best i can but i dont want to put excessive coding on my script because i wouldn't remember all at once. All i will be doing is to take or put code that are only necessary and only those i can remember and learn[that's the motive]. If i put all the coding of php like exception handling, error handling, session_start() or header() or anything like that all at once i might end up having so much in my brain that i may be not be able to grasp what i was doing and for what. 

 

I do understand that the coding i am doing is not well formed and organised but I would keep developing a habit to improve it with time. 

Link to comment
Share on other sites

I have added "values" by putting dns records as variables in values but now i m seeing that 

if(isset($u992092914_trial))// check the db exists
{  

$host= $dnsinfo['host'];
$class = $dnsinfo['class'];
$ttl= $dnsinfo['ttl'];
$type= $dnsinfo['type'];
$pri = $dnsinfo['pri'];
$target= $dnsinfo['target'];

$user_query= "INSERT INTO u992092914_trial( host, class, ttl, type, ip address, rname, mname, refresh, retry, expire, min ttl, txt), 
VALUES('$target', '$class', '$ttl', '$type', '$pri', '$target')"; 
}
if($myconn->query($user_query ===true)){
echo "Tables created successfully";
} else {
echo "tables cant be created." .$myconn->error;
}
$myconn->close();
?>
Warning: mysqli::query(): Empty query in /home/u992092914/public_html/b/mysql.php on line 79
tables cant be created.

I added example.com in the form and it processed it well dns record for it perfectly but it tables are not getting created. 

Link to comment
Share on other sites

$user_query= "INSERT INTO u992092914_trial( host, class, ttl, type, ip address, rname, mname, refresh, retry, expire, min ttl, txt),
VALUES('$target', '$class', '$ttl', '$type', '$pri', '$target')"
;

 

There is 12 column names and only 6 values, you would have to set any to NULL values in the database if wanted to insert with a blank value

These also have to match the data in their same orders.

 

You are better off to insert only what has actual values.

 

You may also want to browse the reserved words for mysql on this page so do not use them, or must `backtick` them

https://dev.mysql.com/doc/refman/5.7/en/keywords.html

host,type,expire are for sure reserved words

 

Also suggest not using spaces for column names and use an underscore instead.

Prepending them could work, such as my_columnname

Link to comment
Share on other sites

Warning: mysqli::query(): Empty query ...

 

in addition to the problems in your sql statement itself, the error you are getting is because your line of code that's running your query is incorrect.

 

this is your line of code for running the query in the last post in this thread -

if($myconn->query($user_query ===true)){

 

this is the equivalent line of code from the 1st, 4th and 6th posts in this thread -

if($myconn->query($user_query)===TRUE){

 

look at those two lines of code carefully and find what's different between them. this is why in one of your threads, someone told you not to just copy/paste things you have seen somewhere. copy/pasting things doesn't teach you what they mean.

 

your line of code is doing three things -

 

1) it contains an if(...){ conditional statement to test if an expression is true.

 

2) it's calling a mysqii database method to execute a sql query statement - $myconn->query($user_query)

 

3) it's comparing the value returned from the msyqli ->query() method with an exact TRUE value - ===TRUE. the result of this comparison is then being tested by the if(...){ conditional statement.

 

this 3rd part is completely unnecessary and is just adding clutter to your code, which may be the reason the line of code you wrote in post #9 is incorrect. you are writing more code than is needed, making it difficult to see what you are actually doing.

 

there's an expression we have in English - you cannot see the forest for the trees, which means you cannot see the big picture of what you are trying to accomplish because you are getting stuck on the small details. there's another expression/acronym - KISS (Keep It Simple Stupid.) which means to use the simplest method that works and don't overly complicate something.

 

since all you need are parts #1 and #2, an if(){ conditional statement and a mysqli ->query() method call, this is all you need to do to call and test if the mysqli ->query() method returned a true value -

if($myconn->query($user_query)){

by actually learning the meaning of what you are doing, you can write simple, error free code that does what you want.

Link to comment
Share on other sites

Regarding TRUE and true, which i actually did to see if anything wrong goes with the both or both are considered same. It was not like copy/paste, if it was, you would have seen more perfection or error free coding than you are seeing. Errors are so much because I m at learning phase. 

 

I checked and removed the  TRUE from it ...[ if($myconn->query($user_query ===TRUE)) but i m still seeing this error

 

I have also changed the name of columns and values and also checked if anyone of them is reserved word or not and Used only 3 columns and  3 values for that to insert data in databases but no success

Warning: mysqli::query(): Empty query ...

I guess the reason might be that only "url" is considered from the form to be inserted into the tables but not the dns records which i am outputting via php coding  from the form itself and is trying to add insert into databases. 

Link to comment
Share on other sites

$user_query only gets a value if this condition is true

if(isset($u992092914_trial))

If it is not true you attempt the query anyway on an empty string.

 

Where is $u992092914_trial being set?

$u992092914_trial : this is actually a database name. i used it to check if database exists/ is set or not. Am i doing it wrong?

Link to comment
Share on other sites

 

isset() is for checking if PHP variables exist, not for external physical entities such as a database.

 

If the database didn't exist your connection code would fail EG

$myconn = new mysqli($servername, $username, $password, $dbname);

Thanks. 

There are few variable to check for as i m looking to get output via $dnsinfo, so I think that $dnsinfo should be placed here. But I was getting the same error. 

Then i tried every php variable used on coding, $host, $result, but result always shows said above error. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.