Hi

My first post yaa lol. This will lily be the 1st of many posts as i trty to get my head round things.

Basically i am going to need to post info from a form in to a database but all so post it to a remote api url at the same time how best to do this can it be done.

example.

FORM
[USERNAME]
[SUBMIT BUTTON]

i NEED TO INSERT THE USER NAME IN TO THE DATABASE AND ALL SO SEND IT TO

http://www.apidomain.com/?user=USERNAME

Hope i have explained what i need well.

All so why i am here what is the best way to auto increment the form i now there will be an easy way to do this in php but if any of you can tell me what it is or the best way to implement it that would be great.

Dani AI

Generated

Short version: let the database assign the auto‑increment id, read that id back in your PHP after the INSERT, then send the id (and any other fields) to the remote API from the server — not by sending the visitor away. That keeps control, lets you handle failures, and avoids exposing API keys or relying on client JS. This addresses 's concern about the id needing to come from the "form" (it should come from the DB) and gives a safer alternative to the on‑page redirect idea mentioned by .

Example flow and a compact PHP pattern (uses PDO and cURL; prepared statements prevent injection):

<?php
// insert with PDO
$pdo = new PDO('mysql:host=HOST;dbname=DB', 'USER', 'PASS', [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('INSERT INTO users (username) VALUES (:u)');
$stmt->execute([':u'=>$username]);
$id = $pdo->lastInsertId();

// send server-to-server to API
$ch = curl_init('https://api.example.com/receive');
curl_setopt_array($ch, [
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => http_build_query(['id'=>$id, 'username'=>$username]),
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$curlErr = curl_error($ch);
curl_close($ch);

Practical points missing from earlier replies: wrap the DB insert and follow-up logic in a try/catch. Decide transactional behavior: if the API must accept the record before you consider it "saved", use a DB transaction or insert then mark the row with a status column (e.g., pending -> sent -> confirmed). Log API responses and build a retry queue (cron or job worker) for transient failures. Prefer POST over GET for larger payloads and use HTTPS.

Security and reliability: use prepared statements (safer than manual replacements suggested by ), validate inputs, never put API secrets in client code, and check both curl errors and API response codes. Read the PHP docs for PDO::lastInsertId and the cURL extension for implementation details.

Recommended Answers

All 4 Replies

Ok, first off I'd direct my form to a page of my own, say "submit.php", which would store the data passed to it into the database and then (before echoing any data to the page) use header() to redirect the user to the api url.

I'm assuming you know ho to connect to a database and submit the data. If now, search the web for a tutorial, there's millions.

Use <form action="submit.php" method="post">... to submit the username to submit.php, whose code is below:

<?php
//send the info to this page via POST method.
$username = $_POST['username'];

//connect to database a run sql "INSERT INTO" query.

$loc = 'location: www.apidomain.com?username=' . $username;
header($loc);
//that last line redirects the user to the api site submitting the username.
?>

Alternatively, you could embed the apidomain.com in a frame or something like that to keep the user on your page, or add "&return=www.yourdomain.com/done.php" to $loc and configure the api to return the user to the value specified.

With the auto increment thing, what is it that you want to auto increment? The id, in the table, of the username submitted? If so, set up a column to auto increment when you create the SQL table.

(If none of this is making sense then look at some tutorials for forms, MySQl tables etc. with this stuff in mind)

Hope that helped.

Before you do anything with the data, you need to sanatize it. This means get rid of characters hackers can use to invade your server.

At a bare minimum,
<code=PHP>
$_POST[xxxx} = str_replace array('<', '>', '[', ']', '{', '}', '\\'), "", $_POST[xxxx]);
</code>

Common sense comes into play here, if you have an input field, firstname, replace ALL special characters except those that could legitimately be in first name such as a dash or single quote. Use the above code and add ~!@#$%^&*()+=:;"?/

Very carefully check out the special characters used by the database application. For example, in php the single quote can create issues. Use php add_slashes to any data field that could possibly contain a single quote.

Hope this helps you while not totally answering your question.

THANKS GUYS

I need to send the auto increment number via the url to the api so i needs to be done by the form and not the database.

Thanks for the info about removing the nasty hacking stuff. I use a cms that has a class i can call to do it for me same with putting stuff in the database but its all in OOP which is even harder to get to grips with so i may just do it in normal php and use the tips in both the posts.

I don't want to send visitors to the api url i want it to be done from my site but with you saying the iframe i reminded me i have so JavaScript which i use to auto subscribe new members to my mailing list.

Basically i pull the users email from my database in to a hidden form and this JavaScript when it sees an email address it will submit the form to the url i have set i think that could easily be modified to be trigged by some thing else.

Thanks Guys

First u can do like this
?.option=submit&user=$username;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.